Fragment中的TextView为null

时间:2014-06-01 17:11:50

标签: java android xml android-layout android-fragments

我一直试图解决这个问题一段时间了,我已经看了很多问题,但仍然无效。

我有MainActivity SlideMenu,所有项目都打开Fragment。其中一个Fragments扩展为ListFragment。我要做的是,一旦ListItem被选中,另一个Fragment被调用并显示一些数据......现在只是一个标题。

选择ListItem后,我正在创建新Fragment的实例,将数据保存在Bundle中并调用新的Fragment进行显示。

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // do something with the data
    Article a = articleList.get(position);

    Fragment articleFragment = new ArticleFragment();

    Bundle args = new Bundle();
    args.putString("title", a.title);
    articleFragment.setArguments(args);

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(R.id.frame_container, articleFragment);
    transaction.commit();
}

onCreateView的{​​{1}}尝试使用Fragment数据设置TextView的文字。

Bundle's

布局xmls是这两个文件:

activity_article_fragment.xml

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_pages, container, false);

    TextView titleTextView = (TextView) rootView.findViewById(R.id.article_title);

    String title = getArguments().getString("title");
    titleTextView.setText(title);

    return rootView;
}

fragment_article.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="info.androidhive.slidingmenu.ArticleFragment"
    tools:ignore="MergeRootFrame" />

我尝试过尽可能多的建议...使用<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="info.androidhive.slidingmenu.ArticleFragment$PlaceholderFragment" > <TextView android:id="@+android:id/article_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> 从xml(accessing textview within a fragment in activityAndroid : How do I update my textView in a Fragment获取ID)但它仍然是View

虽然调试所有内容工作正常但在尝试设置null的文本TextView时显然会停止。

更新

按照Karakuri的建议做了......正在显示文字,但看起来像这样:

Text over the first <code>ListItem</code> - might be some layout settings?

0 个答案:

没有答案
相关问题