Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.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 在片段中查找ListView?_Android_Android Widget - Fatal编程技术网

Android 在片段中查找ListView?

Android 在片段中查找ListView?,android,android-widget,Android,Android Widget,我试图在片段中找到一个ListView,以便能够通过创建它的主活动添加和删除项。下面是我的尝试(我基于com.viewpagerindicator.sample),但当我最终尝试访问mainListView时,它崩溃了 public final class ContactListFragment extends Fragment { private ListView MyListView; public ListView GetListView() {

我试图在片段中找到一个ListView,以便能够通过创建它的主活动添加和删除项。下面是我的尝试(我基于com.viewpagerindicator.sample),但当我最终尝试访问mainListView时,它崩溃了

public final class ContactListFragment extends Fragment {
    private ListView MyListView;

    public ListView GetListView()
    {
        return MyListView;
    }


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        LinearLayout theLayout = (LinearLayout) inflater.inflate(R.layout.tab_frag, container, false);
        MyListView = (ListView)theLayout.findViewById(R.id.listViewx);
        MyListView.setAdapter(adapter);     
        return theLayout;
    }
}
    <ListView
        android:id="@+id/listViewx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>
这里是布局图

    <ListView
        android:id="@+id/listViewx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

这是我想要访问listview的主要活动的代码,以便能够修改 公共类ContactListActivity扩展了BaseSampleActivity {

    <ListView
        android:id="@+id/listViewx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_选项卡);
列表片段=新向量();
add(Fragment.instantiate(这是ContactListFragment.class.getName());
add(Fragment.instantiate(这是ContactListFragment.class.getName());
add(Fragment.instantiate(这是ContactListFragment.class.getName());
PagerAdapter mAdapter=新的PagerAdapter(getSupportFragmentManager(),fragments);
ViewPager mPager=(ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
TabPageIndicator mIndicator=(TabPageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
ContactListFragment XCLF=(ContactListFragment)fragments.get(1);
ListView mainListView=(ListView)XCLF.GetListView();
}

任何帮助都将不胜感激,谢谢

我也遇到了这个问题。为了解决它,我保留了对布局的引用,而不是特定视图。然后当我需要返回特定的
视图时(即,当您调用
getListView()时)
您必须检查对布局的引用是否为空。如果为空,请将其充气、设置,然后找到所需的视图并返回

    <ListView
        android:id="@+id/listViewx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>
然后在
onCreateView()
方法中检查布局是否已设置。如果布局已设置好,只需返回该布局,否则将其充气,并像现在一样进行设置

    <ListView
        android:id="@+id/listViewx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

通过这种方式,您可以确保它只充气一次,并且在需要时总是充气

您无法在
活动
onCreate()
中获得
片段的视图,因为
片段的布局在此阶段没有充气。请使用
片段
onActivityCreated()
更新它的
列表视图

    <ListView
        android:id="@+id/listViewx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

请参见

它仍然始终为空,我甚至尝试使用相同的方法创建一个TextView,在片段上声明并创建onCreateView,使用(new TextView(getActivity())并从GetTextView()返回它(与上面的getListView相同)它总是返回null。我按照那篇文章的建议做了,但现在的问题是,当我从ViewPagerIndicator小部件中的片段移动2页,然后返回到第一页时,ListView是空的,我尝试调用“Adapter.notifyDataSetChanged()”,但一旦调用它,应用程序就会崩溃。
    <ListView
        android:id="@+id/listViewx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>