0

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

单击列表项开始生成一个大的可打包项,完成后应移动到一个新活动(在 TabSwipe 容器之外),将可打包项作为 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 {

这在移动到片段之前运行良好,并且仍然可以正常运行,直到我们到达 moveToActivity() 的 startActivity(i) 行;它在哪里抛出这个错误:

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

“未找到”类是 my_new.class,它肯定存在。确实,如果我添加

getActivity().finish();

startActivity(i);

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

澄清; 父母是

public class myTabsActivity extends TabSwipeActivity {

TabSwipeActivity 是

public abstract class TabSwipeActivity extends SherlockFragmentActivity {

我正在处理的片段是

public class myClassFragment extends SherlockListFragment {

我可能错过了片段中的工具吗?

4

2 回答 2

0

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

希望能帮助到你!

于 2013-05-02T01:05:31.663 回答
0

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

/* ... */
Context ctx;

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

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

希望能帮助到你。

于 2013-05-02T00:37:42.590 回答