您好,当我启动抽屉活动片段时,弹出非法状态异常内容视图尚未创建错误。这是我的代码和错误。我正在使用自定义列表适配器。
error:
10-14 09:40:25.926: E/AndroidRuntime(6736): java.lang.IllegalStateException: Content view not yet created
10-14 09:40:25.926: E/AndroidRuntime(6736): at android.app.ListFragment.ensureList(ListFragment.java:386)
10-14 09:40:25.926: E/AndroidRuntime(6736): at android.app.ListFragment.getListView(ListFragment.java:280)
10-14 09:40:25.926: E/AndroidRuntime(6736): at com.example.fragments.HomeFragment$1.done(HomeFragment.java:74)
我的 oncreateview
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_home, container, false);
listview=(ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
onResume 中的 mysetlist 适配器调用
@Override
public void onResume() {
super.onResume();
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Shopinfo");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> Shopinfo, ParseException e) {
// TODO Auto-generated method stub
if(e==null){
mShop=Shopinfo;
String[] spots = new String[mShop.size()];
int i = 0;
for(ParseObject Shopinfos : mShop) {
spots[i] = Shopinfos.getString(ParseConstants.KEY_SHOP_NAME);
i++;
}
if (getListView().getAdapter() == null) {
adapter=new ShopListAdapter(list.getContext(), mShop);
setListAdapter(adapter);
}
else {
((ShopListAdapter)getListView().getAdapter()).refill(mShop);
}
}
}
});
}
将以下内容移至onActivityCreated()
方法或在onViewCreated()
.
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Shopinfo");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> Shopinfo, ParseException e) {
// TODO Auto-generated method stub
if(e==null){
mShop=Shopinfo;
String[] spots = new String[mShop.size()];
int i = 0;
for(ParseObject Shopinfos : mShop) {
spots[i] = Shopinfos.getString(ParseConstants.KEY_SHOP_NAME);
i++;
}
if (getListView().getAdapter() == null) {
adapter=new ShopListAdapter(list.getContext(), mShop);
setListAdapter(adapter);
}
else {
((ShopListAdapter)getListView().getAdapter()).refill(mShop);
}
}
}
});
}
一个好的做法是将所有使用 UI 小部件的东西都放在里面onActivityCreated()
method.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)