6

すべてが順調に進んでいました。しかし、私のアプリでいくつかのテストを行った後。クラッシュし、Logcat に「コンテンツ ビューが作成されていません」と表示されます。私のコードは少し異なります。私は 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) {
            }

        }

私のログキャット:

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)
4

1 に答える 1

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() ; 
}
于 2012-11-21T18:57:39.087 に答える