我已经阅读了很长一段时间的帖子,但我没有看到任何描述我遇到的问题的内容。当我在安装后第一次打开我的应用程序时,一切正常,但是当我点击后退按钮退出应用程序然后再次打开它时,它失败并显示“不幸的是,我的应用程序已停止”。弹出对话框。我点击确定,然后一旦我再次打开应用程序,它就会工作......我有一个简单的回调函数,我在其中传递片段管理器。一旦我的异步任务完成,它就会调用回调并启动片段。完整代码如下
updateList(MainActivity.this, getSupportFragmentManager())
.
.
.
public void updateList(Activity activity, FragmentManager fragmentMgr)
{
MyCallback callback = new MyCallback(fragmentMgr);
MyAsyncTask myTask = new MyAsyncTask(activity, callback);
myTask.execute();
}
private class MyAsyncTask extends AsyncTask<String, Void, String>
{
private ProgressDialog progressDialog;
private MyCallback callback;
MyAsyncTask(Activity activity, MyCallback callback) {
// Init the progress dialog
this.callback = callback;
progressDialog = new ProgressDialog(activity);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
protected void onPreExecute() {
this.progressDialog.setMessage("Loading...");
this.progressDialog.show();
}
@Override
protected String doInBackground(String... input) {
// Update list...
}
protected void onPostExecute(String result) {
progressDialog.dismiss();
callback.run(result);
}
}
public class MyCallback{
private FragmentManager fragmentManager;
public UpdateListCallback(FragmentManager fragmentManager)
{
this.fragmentManager = fragmentManager;
}
@Override
public void run(String result) {
// update fragment with results...
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContent, new ListFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commitAllowingStateLoss();
}
}
我传递给我的 AsyncTask 的活动是一个 FragmentActivity。
这非常有效,直到我退出应用程序,通过后退按钮直到我进入主屏幕。当我再次打开我的应用程序时,它会尝试加载 progressDialog 但它无法提供
07-09 15:42:53.030: E/AndroidRuntime(26915): FATAL EXCEPTION: main
07-09 15:42:53.030: E/AndroidRuntime(26915): java.lang.RuntimeException: Unable to start activity ComponentInfo{myapp/myapp.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41d54798 is not valid; is your activity running?
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.os.Looper.loop(Looper.java:137)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-09 15:42:53.030: E/AndroidRuntime(26915): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 15:42:53.030: E/AndroidRuntime(26915): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 15:42:53.030: E/AndroidRuntime(26915): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-09 15:42:53.030: E/AndroidRuntime(26915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-09 15:42:53.030: E/AndroidRuntime(26915): at dalvik.system.NativeStart.main(Native Method)
07-09 15:42:53.030: E/AndroidRuntime(26915): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41d54798 is not valid; is your activity running?
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.view.ViewRootImpl.setView(ViewRootImpl.java:706)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:345)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.view.Window$LocalWindowManager.addView(Window.java:554)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.app.Dialog.show(Dialog.java:277)
07-09 15:42:53.030: E/AndroidRuntime(26915): at myapp.MyList$MyAsyncTask.onPreExecute(MyList.java:313)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.os.AsyncTask.execute(AsyncTask.java:534)
07-09 15:42:53.030: E/AndroidRuntime(26915): at myapp.MyList.updateMyList(DeviceList.java:279)
07-09 15:42:53.030: E/AndroidRuntime(26915): at myapp.DbAdapter.PopulateMyList(DbAdapter.java:323)
07-09 15:42:53.030: E/AndroidRuntime(26915): at myapp.MainActivity.onCreate(MainActivity.java:42)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.app.Activity.performCreate(Activity.java:5206)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
07-09 15:42:53.030: E/AndroidRuntime(26915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)