一切都运行良好.但经过我的应用程序的一些测试后.它崩溃了,在我的Logcat中我看到这个“内容视图尚未创建”经过一些谷歌搜索,我发现我应该将我的listadapter放入onActivityCreated.我的代码有点不同,我使用的是lazyadapter,所以我觉得我需要一些建议.请帮我.
public class AndroidFragment extends SherlockListFragment implements ActionBar.TabListener{ static final String URL = "https://xml.xml"; static final String KEY_SONG = "song"; static final String KEY_ID = "id"; static final String KEY_TITLE = "title"; static final String KEY_CAT_ARTIST = "artistcat"; static final String KEY_DURATION = "duration"; static final String KEY_THUMB_URL = "thumb_url"; static final String KEY_BIG_URL = "big_url"; static final String KEY_CAT_URL = "cat_url"; static final String KEY_DESC = "cat_desc"; ArrayList<HashMap<String,String>> menuItems; ListAdapter adapter; Context appContext; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){ appContext = inflater.getContext().getApplicationContext(); new loadListView().execute(); return super.onCreateView(inflater,container,savedInstanceState); } public class loadListView extends AsyncTask<Integer,String,String> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(Integer... args) { // updating UI from Background Thread menuItems = new ArrayList<HashMap<String,String>>(); XMLParser parser = new XMLParser(); String xml = parser.getXmlFromUrl(URL); // getting XML Document doc = parser.getDomElement(xml); // getting DOM element NodeList nl = doc.getElementsByTagName(KEY_SONG); // looping through all item nodes <item> for (int i = 0; i < nl.getLength(); i++) { // creating new HashMap HashMap<String,String> map = new HashMap<String,String>(); Element e = (Element) nl.item(i); // adding each child node to HashMap key => value map.put(KEY_ID,parser.getValue(e,KEY_ID)); map.put(KEY_TITLE,KEY_TITLE)); map.put(KEY_CAT_ARTIST,KEY_CAT_ARTIST)); map.put(KEY_DURATION,KEY_DURATION)); map.put(KEY_THUMB_URL,KEY_THUMB_URL)); map.put(KEY_BIG_URL,KEY_BIG_URL)); map.put(KEY_CAT_URL,KEY_CAT_URL)); map.put(KEY_DESC,KEY_DESC)); // adding HashList to ArrayList menuItems.add(map); } return null; } @Override protected void onPostExecute(String args) { if (getActivity() != null) { adapter=new MainPageLazyAdapter(getActivity(),menuItems); setListAdapter(adapter); } } } @Override public void onTabSelected(Tab tab,FragmentTransaction ft) { ft.add(android.R.id.content,this); ft.attach(this); } @Override public void onTabUnselected(Tab tab,FragmentTransaction ft) { ft.detach(this); } @Override public void onTabReselected(Tab tab,FragmentTransaction ft) { } }
我的LOGCAT:
10-08 17:29:32.946: E/AndroidRuntime(6376): FATAL EXCEPTION: main 10-08 17:29:32.946: E/AndroidRuntime(6376): java.lang.IllegalStateException: Content view not yet created 10-08 17:29:32.946: E/AndroidRuntime(6376): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328) 10-08 17:29:32.946: E/AndroidRuntime(6376): at android.support.v4.app.ListFragment.getListView(ListFragment.java:222) 10-08 17:29:32.946: E/AndroidRuntime(6376): at in.wptrafficanalyzer.actionbarsherlocknavtab.AndroidFragment$loadListView.onPostExecute(AndroidFragment.java:108) 10-08 17:29:32.946: E/AndroidRuntime(6376): at in.wptrafficanalyzer.actionbarsherlocknavtab.AndroidFragment$loadListView.onPostExecute(AndroidFragment.java:1) 10-08 17:29:32.946: E/AndroidRuntime(6376): at android.os.AsyncTask.finish(AsyncTask.java:602) 10-08 17:29:32.946: E/AndroidRuntime(6376): at android.os.AsyncTask.access$600(AsyncTask.java:156) 10-08 17:29:32.946: E/AndroidRuntime(6376): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) 10-08 17:29:32.946: E/AndroidRuntime(6376): at android.os.Handler.dispatchMessage(Handler.java:99) 10-08 17:29:32.946: E/AndroidRuntime(6376): at android.os.Looper.loop(Looper.java:137) 10-08 17:29:32.946: E/AndroidRuntime(6376): at android.app.ActivityThread.main(ActivityThread.java:4512) 10-08 17:29:32.946: E/AndroidRuntime(6376): at java.lang.reflect.Method.invokeNative(Native Method) 10-08 17:29:32.946: E/AndroidRuntime(6376): at java.lang.reflect.Method.invoke(Method.java:511) 10-08 17:29:32.946: E/AndroidRuntime(6376): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982) 10-08 17:29:32.946: E/AndroidRuntime(6376): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749) 10-08 17:29:32.946: E/AndroidRuntime(6376): at dalvik.system.NativeStart.main(Native Method)
解决方法
我认为这应该可以解决你的问题.
@Override public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState){ View view = inflater.inflate(R.layout.layout,null); return view //You must return your view here } @Override public void onResume() { //onResume happens after onStart and onActivityCreate new loadListView().execute(); super.onResume() ; }