我在getListView()
的OnActivityCreated()
中呼叫ListFragment
。它工作正常,但是如果我旋转设备屏幕并再次调用getListView(),它将返回null。
这是ListFragment代码:
public class MyListFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.my_list_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ListView list = getListView();
if (list != null) {
//List is not null!
}
else {
//List is null, there is an error.
}
}
}
这是布局xml(my_list_fragment.xml):
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
最佳答案
我认为您应该在onViewCreated()中调用它:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ListView list = getListView();
...
}
关于android - 屏幕旋转后,ListFragment getListView()返回null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19755477/