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);
}
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();
}
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 .
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.