gpt4 book ai didi

android - 无法在 ListFragment 中创建自定义 ListView

转载 作者:行者123 更新时间:2023-11-29 15:57:41 24 4
gpt4 key购买 nike

我导入了库 [1]:https://github.com/shontauro/android-pulltorefresh-and-loadmore

示例类正在扩展 ListActivity 并且一切正常。 我重做了 extends ListFragment,但出现错误。

 public class SurveyGuestsList extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.loadmore, null);

arrayList = new ArrayList<SurveyGuestsListBean>();
context = getActivity();
new LoadDataTask().execute();

// set a listener to be invoked when the list reaches the end
((LoadMoreListView) getListView())
.setOnLoadMoreListener(new OnLoadMoreListener() {
public void onLoadMore() {
// Do the work to load more items at the end of list
// here
new LoadDataTask().execute();
}
});
return v;
}

错误:

11-18 07:34:10.508    1777-1777/com.example.MANAGMENT E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at com.example.MANAGMENT.fragments.SurveyGuestsList.onCreateView(SurveyGuestsList.java:50)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

编辑:我把代码移到了这里,它起作用了!!!

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
((LoadMoreListView) getListView())
.setOnLoadMoreListener(new OnLoadMoreListener() {
public void onLoadMore() {
// Do the work to load more items at the end of list here
new LoadDataTask().execute();
}
});
super.onViewCreated(view, savedInstanceState);
}

最佳答案

fragment 和 Activity 有不同的生命周期和回调。对于 fragment ,onCreateView 用于创建 View ,直到您返回 v 才附加到 View 层次结构,因此您不能在其中调用 getListView。只需按如下方式创建并返回您的 View 。

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.loadmore, null);
return v;
}

将其余代码移至 onViewCreated()或稍后的回调。

关于android - 无法在 ListFragment 中创建自定义 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26989058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com