Android AsyncTask退出异常

时间:2012-10-05 16:23:02

标签: android android-asynctask

我的Asynctask问题。每当我用我的BACK按钮退出我的应用程序时,它就会发现异常。请看一下我的代码。请帮我纠正一下。感谢。

AndroidFragment.java

    public class AndroidFragment extends SherlockListFragment  implements ActionBar.TabListener{

    private ProgressDialog pDialog;
    static final String URL = "https://dl.dropbox.com/s/86p8y5y2birua9n/test1.xml";
    // XML node keys
    static final String KEY_ITEM = "item"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_NAME = "name";
    static final String KEY_COST = "cost";
    static final String KEY_DESC = "description";
    ArrayList<HashMap<String, String>> menuItems;
    ListAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        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_ITEM);
            // 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_NAME, parser.getValue(e, KEY_NAME));
                map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
                map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

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




                    return null;
                }   


        @Override
        protected void onPostExecute(String args) {


            adapter = new SimpleAdapter(getActivity(), menuItems,
                    R.layout.list_item,
                    new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                            R.id.name, R.id.desciption, R.id.cost });


            setListAdapter(adapter);
            //pDialog.dismiss();
            }
        }

     @Override
     public void onDestroy() {
         if (adapter  != null) {
         setListAdapter(null);
         }
         super.onDestroy();
     }

    @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-06 00:17:55.750: E/AndroidRuntime(8780): FATAL EXCEPTION: main
10-06 00:17:55.750: E/AndroidRuntime(8780): java.lang.NullPointerException
10-06 00:17:55.750: E/AndroidRuntime(8780):     at android.widget.SimpleAdapter.<init>(SimpleAdapter.java:85)
10-06 00:17:55.750: E/AndroidRuntime(8780):     at in.wptrafficanalyzer.actionbarsherlocknavtab.AndroidFragment$loadListView.onPostExecute(AndroidFragment.java:96)
10-06 00:17:55.750: E/AndroidRuntime(8780):     at in.wptrafficanalyzer.actionbarsherlocknavtab.AndroidFragment$loadListView.onPostExecute(AndroidFragment.java:1)
10-06 00:17:55.750: E/AndroidRuntime(8780):     at android.os.AsyncTask.finish(AsyncTask.java:602)
10-06 00:17:55.750: E/AndroidRuntime(8780):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
10-06 00:17:55.750: E/AndroidRuntime(8780):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
10-06 00:17:55.750: E/AndroidRuntime(8780):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 00:17:55.750: E/AndroidRuntime(8780):     at android.os.Looper.loop(Looper.java:137)
10-06 00:17:55.750: E/AndroidRuntime(8780):     at android.app.ActivityThread.main(ActivityThread.java:4512)
10-06 00:17:55.750: E/AndroidRuntime(8780):     at java.lang.reflect.Method.invokeNative(Native Method)
10-06 00:17:55.750: E/AndroidRuntime(8780):     at java.lang.reflect.Method.invoke(Method.java:511)
10-06 00:17:55.750: E/AndroidRuntime(8780):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
10-06 00:17:55.750: E/AndroidRuntime(8780):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
10-06 00:17:55.750: E/AndroidRuntime(8780):     at dalvik.system.NativeStart.main(Native Method)

新LOGCAT:

10-06 00:49:26.094: E/AndroidRuntime(10780): FATAL EXCEPTION: main
10-06 00:49:26.094: E/AndroidRuntime(10780): java.lang.NullPointerException
10-06 00:49:26.094: E/AndroidRuntime(10780):    at android.widget.SimpleAdapter.<init>(SimpleAdapter.java:85)
10-06 00:49:26.094: E/AndroidRuntime(10780):    at in.wptrafficanalyzer.actionbarsherlocknavtab.AppleFragment$loadListView.onPostExecute(AppleFragment.java:97)
10-06 00:49:26.094: E/AndroidRuntime(10780):    at in.wptrafficanalyzer.actionbarsherlocknavtab.AppleFragment$loadListView.onPostExecute(AppleFragment.java:1)
10-06 00:49:26.094: E/AndroidRuntime(10780):    at android.os.AsyncTask.finish(AsyncTask.java:602)
10-06 00:49:26.094: E/AndroidRuntime(10780):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
10-06 00:49:26.094: E/AndroidRuntime(10780):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
10-06 00:49:26.094: E/AndroidRuntime(10780):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 00:49:26.094: E/AndroidRuntime(10780):    at android.os.Looper.loop(Looper.java:137)
10-06 00:49:26.094: E/AndroidRuntime(10780):    at android.app.ActivityThread.main(ActivityThread.java:4512)
10-06 00:49:26.094: E/AndroidRuntime(10780):    at java.lang.reflect.Method.invokeNative(Native Method)
10-06 00:49:26.094: E/AndroidRuntime(10780):    at java.lang.reflect.Method.invoke(Method.java:511)
10-06 00:49:26.094: E/AndroidRuntime(10780):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
10-06 00:49:26.094: E/AndroidRuntime(10780):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
10-06 00:49:26.094: E/AndroidRuntime(10780):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

问题是您的列表需要花费很多时间来加载和解析。

时,您的活动已被销毁
protected void onPostExecute(String args)

被召唤。

但是如果活动已经被销毁,那么您不需要更新UI。最简单的解决方案是:

@覆盖         protected void onPostExecute(String args){

        if (getActivity() != null) {
            ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,
                R.layout.list_item,
                new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                        R.id.name, R.id.desciption, R.id.cost });


            setListAdapter(adapter);
               //pDialog.dismiss();

        }
    }

答案 1 :(得分:0)

我的猜测是,在创建简单适配器时,片段未附加到活动。这是您的新SimpleAdapter调用中唯一可以返回null的东西。将asynctask请求移动到onViewCreated,看看会发生什么。

更新: 我检查了SimpleAdapter文件中的源代码。抛出错误的一行是尝试从传入的上下文(活动)创建一个LayoutInflater。返回null,所以你的getactivity()调用返回null。

解答: 如果未附加片段,getActivity()将返回null,请使用应用程序上下文创建SimpleAdapter。您可以从LayoutInflater传递给onCreateView。

获取应用程序上下文

答案 2 :(得分:0)

在Class Level声明Listadapter适配器

   @Override
        public void onDestroy() {
            if (adapter  != null) {
            setListAdapter(null);
            }
            super.onDestroy();
        }