gpt4 book ai didi

android - 进度对话框坏 token 异常

转载 作者:行者123 更新时间:2023-11-29 01:51:45 33 4
gpt4 key购买 nike

我已经阅读这些帖子很长时间了,但我还没有看到任何描述我遇到的问题的内容。当我在安装后第一次打开我的应用程序时,一切正常,但是当我点击后退按钮退出应用程序然后再次打开它时,它失败并显示“不幸的是,我的应用程序已停止。”对话框弹出。我点击确定,然后一旦我再次打开应用程序,它就可以工作了……我有一个简单的回调函数,我在其中传递 fragment 管理器。一旦我的异步任务完成,它就会调用回调并启动 fragment 。完整代码如下

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 的 Activity 是 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)

最佳答案

不要在Async Task中创建ProgressDialog progressDialog,在Activity中创建并作为参数传递给Async Task

关于android - 进度对话框坏 token 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17550487/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com