Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 另一个findFragmentById返回null_Android_Fragment - Fatal编程技术网

Android 另一个findFragmentById返回null

Android 另一个findFragmentById返回null,android,fragment,Android,Fragment,我需要获取一个片段(ListFragment),但下面的代码返回null。编辑:我应该补充一点,我正在使用ViewPager和FragmentStatePagerAdapter TaskListFragment tlf = (TaskListFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_task_list); 我猜这是因为id是组成片段的XML文件中RelativeLayout的id 我发现了一个“Fr

我需要获取一个片段(ListFragment),但下面的代码返回null。编辑:我应该补充一点,我正在使用ViewPager和FragmentStatePagerAdapter

TaskListFragment tlf = (TaskListFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_task_list);
我猜这是因为id是组成片段的XML文件中RelativeLayout的id

我发现了一个“Fragment”标记,认为这应该使我能够为片段提供一个id,但在XML文件中包含该标记会给我一个“错误膨胀类片段”错误,即使eclipse显示XML是有效的

我该怎么办?问题是id错误吗?如果是这样,我如何给片段一个可以使用findFragmentById检索的id

编辑:添加了XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_task_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<TextView
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="no data" />

</RelativeLayout>


在添加到片段管理器时为片段提供标记,后者使用标记访问它可能与我使用的ViewPager/FragmentStatePagerAdapter重复,因此我想我应该在FragmentStatePagerAdapter中执行此操作?请发布布局xml文件好吗?谢谢。添加了XML的当前版本。