一切都运行良好。但经过我的应用程序的一些测试后。它崩溃了,在我的Logcat中,我看到了这个" Content View Not Yet Created"经过一些谷歌搜索后,我发现我应该将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, parser.getValue(e, KEY_TITLE));
map.put(KEY_CAT_ARTIST, parser.getValue(e, KEY_CAT_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
map.put(KEY_BIG_URL, parser.getValue(e, KEY_BIG_URL));
map.put(KEY_CAT_URL, parser.getValue(e, KEY_CAT_URL));
map.put(KEY_DESC, parser.getValue(e, 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)
答案 0 :(得分:8)
我认为这可以解决你的问题。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 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() ;
}