gpt4 book ai didi

android - AsynchTask 中的 progressBar 导致崩溃, "Unable to add window -- token null is not for an application"

转载 作者:行者123 更新时间:2023-11-29 15:13:05 27 4
gpt4 key购买 nike

在我的应用程序中,我试图在连接到服务器时显示忙碌指示器,因此我创建了 AsynchTask with dialog with progressBar.

在运行时,当我尝试连接到服务器时,应用程序崩溃,从下面发布的 LogCat 看来它是 dialog 的问题,但我不知道究竟是什么导致了错误。

应用程序处于 LandScape 模式,带有 navigationdrawer

请查看onPreExecute 方法和logcat 错误。

onPreExecute:

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = new Dialog(getApplicationContext());
dialog.setCancelable(false);
//dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.progressdialog);
progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar1);
dialog.show();
}

LogCat:

 E/AndroidRuntime(20575): Process: com.example.mqtt_designlayout_02, PID: 20575
E/AndroidRuntime(20575): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime(20575): at android.view.ViewRootImpl.setView(ViewRootImpl.java:731)
E/AndroidRuntime(20575): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:278)
E/AndroidRuntime(20575): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
E/AndroidRuntime(20575): at android.app.Dialog.show(Dialog.java:288)
E/AndroidRuntime(20575): at com.example.mqtt_designlayout_02.MainActivity$BusyIndicator.onPreExecute(MainActivity.java:768)
E/AndroidRuntime(20575): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
E/AndroidRuntime(20575): at android.os.AsyncTask.execute(AsyncTask.java:535)
E/AndroidRuntime(20575): at com.example.mqtt_designlayout_02.MainActivity.connectionIndicator(MainActivity.java:468)
E/AndroidRuntime(20575): at com.example.mqtt_designlayout_02.MainActivity.MQTTConnect(MainActivity.java:460)
E/AndroidRuntime(20575): at com.example.mqtt_designlayout_02.MainActivity.MQTT_Connection_Module(MainActivity.java:558)
E/AndroidRuntime(20575): at com.example.mqtt_designlayout_02.MainActivity.access$0(MainActivity.java:548)
E/AndroidRuntime(20575): at com.example.mqtt_designlayout_02.MainActivity$1.onClick(MainActivity.java:79)
E/AndroidRuntime(20575): at android.view.View.performClick(View.java:4626)
E/AndroidRuntime(20575): at android.view.View$PerformClick.run(View.java:19293)
E/AndroidRuntime(20575): at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(20575): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(20575): at android.os.Looper.loop(Looper.java:157)
E/AndroidRuntime(20575): at android.app.ActivityThread.main(ActivityThread.java:5293)
E/AndroidRuntime(20575): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(20575): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(20575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
E/AndroidRuntime(20575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
E/AndroidRuntime(20575): at dalvik.system.NativeStart.main(Native Method)

对不起,我被禁止了。所以你的解决方案就是更改以下行,dialog = new Dialog(getParent());

最佳答案

看看这篇博文:http://possiblemobile.com/2013/06/context/ ,然后向下滚动显示不同上下文之间比较的位置。

您正在使用应用上下文:
dialog = new Dialog(getApplicationContext());
显示对话框,但应用程序上下文不能这样做。

改用 Activity 上下文。

更新。引用您的评论:
您可以通过让构造函数接受 Context 参数,将 Activity 实例传递给 AsyncTask

例如:

public class MyAsyncTask extends AsyncTask<>{
private Context context;

public MyAsyncTask(Context context){
this.context = context;
}

// and then in onPreExecute use that context
// dialog = new Dialog(context);
}

然后您从这样的 Activity 中调用您的 AsyncTask:

new MyAsyncTask(this).execute(); 
// 'this' is the current instance of Activity.
// and Activity extends Context, so, it IS a Context.

关于android - AsynchTask 中的 progressBar 导致崩溃, "Unable to add window -- token null is not for an application",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28346458/

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