为什么AsyncTask在切换Fragment时崩溃,但在Activity中没有崩溃

时间:2014-01-02 10:44:28

标签: android android-fragments android-asynctask

很可能我在这里缺少一些基础知识,如果有人可以指出的话。

如果我在一个活动中运行AsyncTask然后切换到另一个Activity,则AsyncTask在后台运行而没有任何问题。

但是现在我有一个Activity和不同的片段,我通过导航抽屉加载,每当我切换到另一个片段时,我的AsyncTasks(在片段中启动)会崩溃。

在SO中搜索,似乎这是预期的行为,但是你能告诉我为什么吗?我从跟踪中看到的,如果我销毁当前的Fragment并加载一个新的,AsynTask在该行中崩溃包含getActivity()。

我的代码:

的活动:

public class MyContainer extends Activity {
.........................
public void displayView(int position) {
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new FragmentA();
            break;
        case 1:
            fragment = new FragmentB();
            break;
        case 2:
            fragment = new FragmentC();
            break;
        default:
            break;
        }
        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();
            mDrawerList.setItemChecked(position, true);
            mDrawerLayout.closeDrawer(drawer);
        }
    }
 }

片段A:

public class FragmentA extends Fragment {
    ...................
    ...................
    private class RemoteConnectivity extends AsyncTask<Integer, String, String>
        NotificationManager mNotifyManager;
        NotificationCompat.Builder mBuilder;
        int mId;
        @Override
    protected void onPreExecute() {
    mNotifyManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(getActivity());
        mBuilder.setContentTitle("Import/Export")
            .setContentText("In progress")
            .setSmallIcon(R.drawable.ic_launcher);
    }

        @Override
        protected String doInBackground(Integer... args) {
            mId = 1;
            ..................
            try 
            {
                JSONArray jArray = new JSONArray(result);
                String[] dataline;
                mylibmandbhandler db = new mylibmandbhandler(getActivity());
                int maxlength = jArray.length();
                for (int i = 0; i < maxlength; i++) 
                {
                    JSONObject json = jArray.getJSONObject(i);
                    cnt = 0;
                    dataline = new String[19];
                    dataline[cnt++] = json.getString("xxxxxx");
                        ......................
                        ......................
                    dataline[cnt++] = json.getString("yyyyy");
                        db.addRecord(new mylibman(dataline));
                    mBuilder.setProgress(maxlength, tot, false);
                    mNotifyManager.notify(mId, mBuilder.build());
                }
                db.close();
            }
            catch (JSONException e) 
            {
                return " E503: Error parsing data.";
            }
        }
        @Override
        protected void onPostExecute(String result1) 
        {
        mBuilder.setContentText("Completed. "+result1).setProgress(0,0,false);
        mNotifyManager.notify(mId, mBuilder.build());
            ((MyContainer)getActivity()).updateCount();
        }
    }
}

错误:

01-02 05:21:00.935: E/AndroidRuntime(2355): FATAL EXCEPTION: main
01-02 05:21:00.935: E/AndroidRuntime(2355): Process: com.migrationdesk.mylibman, PID: 2355
01-02 05:21:00.935: E/AndroidRuntime(2355): java.lang.NullPointerException
01-02 05:21:00.935: E/AndroidRuntime(2355):     at com.migrationdesk.mylibman.ImportExportFragment$RemoteConnectivity.onPostExecute(ImportExportFragment.java:554)
01-02 05:21:00.935: E/AndroidRuntime(2355):     at com.migrationdesk.mylibman.ImportExportFragment$RemoteConnectivity.onPostExecute(ImportExportFragment.java:1)
01-02 05:21:00.935: E/AndroidRuntime(2355):     at android.os.AsyncTask.finish(AsyncTask.java:632)
01-02 05:21:00.935: E/AndroidRuntime(2355):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-02 05:21:00.935: E/AndroidRuntime(2355):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
01-02 05:21:00.935: E/AndroidRuntime(2355):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-02 05:21:00.935: E/AndroidRuntime(2355):     at android.os.Looper.loop(Looper.java:137)
01-02 05:21:00.935: E/AndroidRuntime(2355):     at android.app.ActivityThread.main(ActivityThread.java:4998)
01-02 05:21:00.935: E/AndroidRuntime(2355):     at java.lang.reflect.Method.invokeNative(Native Method)
01-02 05:21:00.935: E/AndroidRuntime(2355):     at java.lang.reflect.Method.invoke(Method.java:515)
01-02 05:21:00.935: E/AndroidRuntime(2355):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-02 05:21:00.935: E/AndroidRuntime(2355):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-02 05:21:00.935: E/AndroidRuntime(2355):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案