Obtaining tweets using a TimelineDelegate

android
fabric
embeddedtweet

#1

I am trying to obtain tweets using a TimelineDelegate object in Android. The tweets are to populate a list when the app runs. My problem is that the tweets returned are null which causes the app to crash. Can anyone recommend a solution?

Here is the specific class that handles retrieving tweets:

package me.smac89.example.navigation.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.ViewGroup;

import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.models.Tweet;
import com.twitter.sdk.android.tweetui.CompactTweetView;
import com.twitter.sdk.android.tweetui.SearchTimeline;
import com.twitter.sdk.android.tweetui.TimelineResult;
import com.twitter.sdk.android.tweetui.internal.TimelineDelegate;

import me.smac89.example.listener.RecyclerViewScrollListener;

public class TweetAdapter extends RecyclerView.Adapter<TweetAdapter.TweetViewHolder>
        implements SwipeRefreshLayout.OnRefreshListener {

    private static final int ITEMS_PER_RESULT = 10;
    private final TimelineDelegate<Tweet> tweetsDelegate;
    private final TweetsCallback cb;
    private final Context context;
    @Nullable
    private OnRefreshFinishedListener refreshListener;

    public TweetAdapter(Context ctx) {
        cb = new TweetsCallback();
        context = ctx;
        tweetsDelegate = new TimelineDelegate<>(new SearchTimeline.Builder()
                .query("%23twitterapi OR %23euro2016")
                .resultType(SearchTimeline.ResultType.RECENT)
                .maxItemsPerRequest(ITEMS_PER_RESULT)
                .build());
        tweetsDelegate.refresh(cb);
    }

    @Override
    public TweetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CompactTweetView tv = new CompactTweetView(context, (AttributeSet) null);
        return new TweetViewHolder(tv);
    }

    @Override
    public void onBindViewHolder(TweetViewHolder holder, int position) {
        Tweet tweet = tweetsDelegate.getItem(position);
        CompactTweetView tv = (CompactTweetView) holder.itemView;
        tv.setTweet(tweet);
    }

    @Override
    public int getItemCount() {
        return tweetsDelegate.getCount();
    }

    @Override
    public void onRefresh() {
        tweetsDelegate.refresh(cb);
    }

    public void setRefreshFinishedListener(@Nullable OnRefreshFinishedListener refreshListener) {
        this.refreshListener = refreshListener;
    }

    public interface OnRefreshFinishedListener {
        void onRefreshFinished();
    }

    static final class TweetViewHolder extends RecyclerView.ViewHolder {

        public TweetViewHolder(CompactTweetView itemView) {
            super(itemView);
        }
    }

    private final class TweetsCallback extends Callback<TimelineResult<Tweet>> {

        @Override
        public void success(Result<TimelineResult<Tweet>> result) {
            notifyDataSetChanged();
            notifyListener();
        }

        @Override
        public void failure(TwitterException exception) {
            notifyListener();
        }

        private void notifyListener() {
            if (refreshListener != null) {
                refreshListener.onRefreshFinished();
            }
        }
    }
}

When I run it, the specific error I get is the following:

07-06 17:46:28.726 1723-1723/me.smac89.example E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: me.smac89.example, PID: 1723
                                                                       java.lang.IllegalArgumentException: Target must not be null.
                                                                           at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
                                                                           at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
                                                                           at com.twitter.sdk.android.tweetui.BaseTweetView.setProfilePhotoView(BaseTweetView.java:676)
                                                                           at com.twitter.sdk.android.tweetui.BaseTweetView.render(BaseTweetView.java:474)
                                                                           at com.twitter.sdk.android.tweetui.CompactTweetView.render(CompactTweetView.java:63)
                                                                           at com.twitter.sdk.android.tweetui.BaseTweetView.setTweet(BaseTweetView.java:430)
                                                                           at me.smac89.example.navigation.adapter.TweetAdapter.onBindViewHolder(TweetAdapter.java:52)
                                                                           at me.smac89.example.navigation.adapter.TweetAdapter.onBindViewHolder(TweetAdapter.java:21)
                                                                           at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
                                                                           at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
                                                                           at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
                                                                           at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
                                                                           at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
                                                                           at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
                                                                           at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
                                                                           at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
                                                                           at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
                                                                           at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
                                                                           at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:596)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                           at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1732)
                                                                           at android.widget.LinearLayout.onLayout(LinearLayout.java:1497)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                           at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                           at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                           at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                           at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                           at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
                                                                           at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
                                                                           at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                           at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                           at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                           at android.view.Choreographer.doCallbacks(Choreographer.java:670)

What do I need to do to make this thing work?


RecyclerView using TimelineDelegate
#2

Thanks for reaching out on this @Taibowest and a quick question. If you run another search where tweets are being returned and you’re not getting null, do you experience the same issue?


#3

Thanks for replying. Yea I just tried a new query "#sharknado3", still getting the same error.


#4

Hi guys, I solved this issue replacing for this:

@Override
public TweetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Tweet tweet = tweetsDelegate.getItem(0);
CompactTweetView tv = new CompactTweetView(context, tweet);
return new TweetViewHolder(tv);
}


#5

Thanks for sharing your solution!


#6

I don’t know how in the world you came up with that solution, but that worked brilliantly! I am a bit short of words right now, but thank you very much!