0

Im making a twitter client for android and Im having a hard time adding the list fragment for timelines (fabric) to my activity. At first it would crash my app but now Ive made a few changes and it doesnt crash but once twitter authenticates and it loads the activity but shows a blank screen. below is my code... any help would be greatly appreciated. Thanks in advance.

Activity

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

/**
 * Created by B on 11/24/2015.
 */
public class TweetClass extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tweet_layout_multi);

    Fragment frag = new Fragment();
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.fragment_timeline_user,frag, "Timeline Fragment");
    transaction.commit();

FragmentTimeline.xml (fragment layout)

`

<TextView
    android:id="@id/android:empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/no_tweets"
    android:gravity="center_horizontal|center_vertical"/>

<ListView android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:divider="#e1e8ed"
    android:dividerHeight="1dp"
    android:drawSelectorOnTop="false"/>

tweet_layout_multi (activty layout)

<?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_timeline_user"
    android:name="com.continuumwear.continuumbrowser.TimelineFragment"
    tools:layout="@layout/fragment_timeline">

</RelativeLayout>

TimelineFragment(fragment)

    import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.twitter.sdk.android.tweetui.SearchTimeline;
import com.twitter.sdk.android.tweetui.TweetTimelineListAdapter;


public class TimelineFragment extends ListFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final SearchTimeline searchTimeline = new SearchTimeline.Builder()
                .query("#twitterflock")
                .build();
        final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(getActivity())
                .setTimeline(searchTimeline)
                .build();
        setListAdapter(adapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_timeline, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }
skeete
  • 71
  • 8

1 Answers1

0

It looks like the problem is that you are trying to add a generic Fragment to your activity rather than the TimeLineFragment.

Instead of:

Fragment frag = new Fragment(); // creates a generic fragment, not your ListFragment

You should be creating an instance of the TimelineFragment that you defined, like this:

TimeLineFragment frag = new TimeLineFragment();

Then you can add it using a FragmentTransaction like you already have done above.

Full code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tweet_layout_multi);

    TimeLineFragment frag = new TimeLineFragment(); // change this line
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.fragment_timeline_user,frag, "Timeline Fragment");
    transaction.commit();
}

Edit 1 - Fabric advice

I realise you may have arrived at this solution through experimentation trying to get Fabric to work so, since I have experience with Fabric, I'll try to help there too.

I haven't used the SearchTimeline or TweetTimelineListAdapter but if it's like loading individual tweets, you need to make sure that the Fabric framework has been initialised with the required Twitter kit for your timeline. Something like the following should happen before trying to use SearchTimeline or TweetTimelineListAdapter:

final TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric fabric = Fabric.with(context, new Twitter(authConfig));

The above will initialise Fabric Twitter with guest authentication. If you need to access a user's timeline (rather than the public firehose), then you'll need to include user authentication - see the Fabric docs.

Edit 2

Following your comment, I looked again at your code and it looks like maybe the id of your <ListView> inside FragmentTimeline.xml may be named incorrectly. From the docs (emphasis mine):

ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)

In your ListFragment layout xml, your ListView is named @id/android:list instead of @android:id/list

Community
  • 1
  • 1
aoemerson
  • 147
  • 9
  • Ive modified the code but its still doing the same thing. I already have the twitter authentication working. Just after it authenticates it loads an empty screen. When I use the timeline code in its own activity with a list view it works I'm trying to use the listfragment so I can have multiple timelines on the same screen – skeete Dec 01 '15 at 17:56
  • @skeete I took another look at your code and see it may be because you have named your `ListView` incorrectly. Please take a look at **Edit 2** above and see if that helps at all. – aoemerson Dec 02 '15 at 04:12
  • @skeete Having said that...reading further in the docs, they have an example which uses an ID of `@id/android:list`! Anyway, worth a try. I'll take another look at what you have to see if there's anything else that could be the problem. – aoemerson Dec 02 '15 at 04:21