无法暂停活动 - nullpointerexception

时间:2013-05-01 23:39:17

标签: android actionbarsherlock android-listfragment

这是由SherlockListFragment(在TabSwipe中)和AsyncTask doInBackground引起的。

单击列表项会开始生成一个大的可分配项,完成后,应该移动到一个新活动(在TabSwipe容器之外),将parcelable作为Bundle传递。

这是异步代码:

public class BackgroundSetUpReportData extends AsyncTask<Integer, Integer, PainReport_Holder> {

    // declare the dialog
    ProgressDialog progressDialog;

    // show the dialog
    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setCancelable(false);
        progressDialog
                .setMessage("Building Summary:\nThis may take some time.\nPlease wait...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    @Override
    protected PainReport_Holder doInBackground(Integer... params) {

        DataLayer d = new DataLayer(getActivity().getBaseContext());

        //lots of adding to the PRH class
        return PRH;
    }

    // update the progress dialog
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }

    // show the data
    protected void onPostExecute(PainReport_Holder result) {
        // hide the dialog
        progressDialog.dismiss();
        moveToActivity();
    }

}

public void moveToActivity() {
    Bundle b = new Bundle();
    b.putParcelable("SummaryData", PRH);
    b.putString("fromDate", fromDate);
    b.putString("toDate", toDate);
    Intent i = new Intent(getActivity(), my_new.class);
    i.putExtras(b);
    startActivity(i);
}

整体课程是:

public class myClassFragment extends SherlockListFragment {

这在移动到片段之前完美地工作,并且仍然正常,直到我们到达moveActivity(i)行的moveToActivity();它抛出这个错误:

05-01 23:41:39.906: E/AndroidRuntime(25684): FATAL EXCEPTION: main
05-01 23:41:39.906: E/AndroidRuntime(25684): java.lang.RuntimeException: Unable to pause activity {[app]/[app].[class]}: java.lang.NullPointerException
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2358)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2315)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2295)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.app.ActivityThread.access$1700(ActivityThread.java:117)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.os.Looper.loop(Looper.java:130)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.app.ActivityThread.main(ActivityThread.java:3687)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at java.lang.reflect.Method.invokeNative(Native Method)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at java.lang.reflect.Method.invoke(Method.java:507)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at dalvik.system.NativeStart.main(Native Method)
05-01 23:41:39.906: E/AndroidRuntime(25684): Caused by: java.lang.NullPointerException
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.app.Activity.performSaveInstanceState(Activity.java:1037)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1181)
05-01 23:41:39.906: E/AndroidRuntime(25684):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2340)
05-01 23:41:39.906: E/AndroidRuntime(25684):    ... 12 more

我做过的其他研究已经建议传递数据中的空条目,但是我已经在调试中检查了PRH类,并且其中没有空值。

更新

我已经逐步完成了AsyncTask线程,它似乎在以下处抛出了一个ClassNotFound异常:

Intent i = new Intent(getActivity(), my_new.class);

'not found'类是my_new.class,肯定存在。的确,如果我添加

getActivity().finish();

startActivity(i);

然后它工作正常,但调用活动不再在后栈中。

澄清;父母是

public class myTabsActivity extends TabSwipeActivity {

TabSwipeActivity是

public abstract class TabSwipeActivity extends SherlockFragmentActivity {

和我正在处理的片段是

public class myClassFragment extends SherlockListFragment {

我是否错过了碎片中的器具?

2 个答案:

答案 0 :(得分:0)

我记得我曾经在片段中获得了AsyncTask中的NLP一次。 我不记得问题是否完全相同但我通过在onPreExecute中引用应用程序上下文来解决它

/* ... */
Context ctx;

@Override
protected void onPreExecute() {
    /* ... */
    ctx = getActivity().getApplicationContext();
    /* ... */
}

并在需要的地方使用它而不是像创建意图的getActivity()。

希望它有所帮助。

答案 1 :(得分:0)

我认为您的问题与getActivity()方法中的moveToActivity()有关。也许您应该使用getParent()getSupportFragmentManager()或其他任何内容。 我认为getActivity()在使用Fragments时返回null。

希望它有所帮助!