2

我的主要活动中托管了两个选项卡(我使用 ActionBarSherlock)。我的片段加载并解析一个遥远的 xml 以填充列表视图。

如果我在片段完全加载时更改设备方向,它会再次加载而不会出现新方向的问题。但是,如果我在片段加载时更改它,则应用程序强制在加载完成时关闭。

难道我做错了什么?

public class PartOne extends SherlockListFragment{

// All static variables
    static final String URL = "http://Y.xml";
    // XML node keys
    static final String KEY_ITEM = "item"; // parent node
    static final String KEY_TITLE = "title";
    static final String KEY_CAT = "category";
    static final String KEY_DESC = "description";


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new BackgroundAsyncTask().execute();
    }

    public class BackgroundAsyncTask extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(String... paths) {

            ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

            XMLParser parser = new XMLParser();
            String xml = parser.getXmlFromUrl(URL); // getting XML

            if (xml != null) {
            Document doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_ITEM);
            // looping through all item nodes <item>
            for (int i = 0; i < nl.getLength(); i++) {
                if(i%2 != 1) {
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);

                // data to string (to modify them)
                String titre = parser.getValue(e, KEY_TITLE);
                String categorie = parser.getValue(e, KEY_CAT);           
                String description = parser.getValue(e, KEY_DESC);

                // adding each child node to HashMap key => value       
                map.put(KEY_TITLE, titre);
                map.put(KEY_CAT, categorie);
                map.put(KEY_DESC, description);

                // adding HashList to ArrayList
                menuItems.add(map);

                                    }       
                                                            }
                            }
            // selecting single ListView item
            ListView lv = getListView();

            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // getting values from selected ListItem
                    String name = ((TextView) view.findViewById(R.id.title)).getText().toString();
                    String cost = ((TextView) view.findViewById(R.id.categ)).getText().toString();
                    String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
                    // Starting new intent
                    Intent in = new Intent(view.getContext(), SingleMenuItemActivity.class);
                    in.putExtra(KEY_TITLE, name);
                    in.putExtra(KEY_CAT, cost);
                    in.putExtra(KEY_DESC, description);
                    startActivity(in);

                }
            });
            return menuItems;
                                                                                    }

        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

            ListAdapter adapter = new SimpleAdapter(getActivity(),
                    result, R.layout.list_item,
                    new String[] {KEY_TITLE, KEY_DESC, KEY_CAT},
                    new int[] {R.id.title, R.id.desciption, R.id.categ});
            setListAdapter(adapter);
        }

    }

日志 :

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
4

1 回答 1

1

在onConfigurationChanged (Configuration newConfig)方法中处理你的任务,像这样

    Thread th=null;
    public void onCreate(bundle savedInstanceState)
    {
      //Initialize layout

      if(th==null) //This is to check whether task is running
       { 
      //Assign your task
      th=new Thread();
      th.start();
       }
    } 

    //handle the same here
    public void onConfigurationChanged (Configuration newConfig)
    {

       //Do the  layout changes
       if(th==null)//This is to check whether task is running
       { 
       //Assign your task
       th=new Thread();
       th.start();
       }

    }
于 2012-10-18T08:04:22.017 回答