我已经阅读了很长一段时间的帖子,但我还没有看到任何描述我遇到的问题的内容。当我在安装后第一次打开我的应用程序时,一切正常但是当我点击后退按钮退出应用程序然后再次打开时它失败了“不幸的是,myapp已经停止了”。对话框弹出。我点击确定,然后一旦我再次打开应用程序就可以了... 我有一个简单的回调函数,我在其中传递片段管理器。一旦我的异步任务完成,它就会调用回调并启动片段。 完整代码位于
之下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)
答案 0 :(得分:1)
不要在异步任务中创建ProgressDialog progressDialog
,在活动中创建它并将其作为参数传递给异步任务
答案 1 :(得分:0)
我在bad token exception
遇到了类似的问题,这是一个Context
漏洞。对话框需要来自前台活动的上下文。
试试这个:
将AsyncTask的参数从Activity
更改为Context
。
updateList(this)
public void updateList(Context ctx)
{
MyAsyncTask myTask = new MyAsyncTask(ctx);
myTask.execute();
}
MyAsyncTask(Context ctx) {
ProgressDialog progressDialog;
protected void onPreExecute() {
// Init the progress dialog
progressDialog = new ProgressDialog(ctx);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
protected void onPostExecute(String result) {
progressDialog.dismiss();
}
}
如果这对您没有帮助,我认为您在onStart
之前调用了AsyncTask。如果是这种情况,请在updateList()
中调用onStart() / onResume()
。
另外我想说的是应该在onPreExecute()中完全初始化ProgressDialog。您只需要将内部类中的ProgressDialog作为字段。
正如Raghunandan已经说过的那样,你应该只调用progressDialog.dismiss()
来删除对话框视图。