Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragments Content View Not Yet Created

Everything was running fine. But after some testing through my app. It crashes and in my Logcat I see this "Content View Not Yet Created " After some googling, I found out that I should put my listadapter into onActivityCreated. My codes are a bit different, I'm using a lazyadapter so I think I need some advise. Please help me.

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) {
            }

        }

MY 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)
like image 353
Android Novice Avatar asked Oct 08 '12 09:10

Android Novice


1 Answers

I think this should solve your problem.

 @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() ; 
}
like image 110
dougcunha Avatar answered Sep 21 '22 16:09

dougcunha