在我的應用程序,我試圖顯示一個繁忙的指標,而我連接到服務器,所以我創建了AsynchTask
與dialog
與progressBar
。progressynar在AsynchTask導致崩潰,「無法添加窗口 - 令牌null不適用於應用程序」
在運行時,當我嘗試連接到服務器時,應用程序崩潰,出現下面發佈的LogCat
它似乎是dialog
的問題,但我不知道是什麼原因導致錯誤。
應用程序是在橫向模式下與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());
好的,但沒有方法,如getActivtycontext(),我不能使用「this」,因爲我在AsynchTask類 – user2121 2015-02-05 14:34:33
查看我更新的答案。 – 2015-02-05 14:40:20
@ user2121您可以使用 - 'MainActivity.this' – 2017-12-09 12:21:02