在进度造成AsynchTask崩溃,"无法添加窗口 - 令牌null不是一个申请表] [英] progressBar in AsynchTask causes crash, "Unable to add window -- token null is not for an application"

查看:198
本文介绍了在进度造成AsynchTask崩溃,"无法添加窗口 - 令牌null不是一个申请表]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我试图展现一个繁忙的指标,当我连接到服务器,所以我创建 AsynchTask 对话框进度

在运行时,当我尝试连接到服务器时,应用程序crashs,走出了贴在下面 LogCat中现在看来,这与<$ C $问题C>对话框,但我不知道究竟是什么导致错误。

应用程序是在横向模式下使用 navigationdrawer

请看看在上preExecute 方法和的logcat 的错误。

在preExecute

  @覆盖
    在preExecute保护无效(){
        // TODO自动生成方法存根
        super.on preExecute();
        对话框=新的对话框(getApplicationContext());
        dialog.setCancelable(假);
        //dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.progressdialog);
        进度=(进度)dialog.findViewById(R.id.progressBar1);
        dialog.show();
    }

LogCat中

  E / AndroidRuntime(20575):工艺:com.example.mqtt_designlayout_02,PID:20575
 E / AndroidRuntime(20575):$ android.view.WindowManager BadTokenException:无法添加窗口 - 令牌null不是一个应用程序
 E / AndroidRuntime(20575):在android.view.ViewRootImpl.setView(ViewRootImpl.java:731)
 E / AndroidRuntime(20575):在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:278)
 E / AndroidRuntime(20575):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
 E / AndroidRuntime(20575):在android.app.Dialog.show(Dialog.java:288)
 E / AndroidRuntime(20575):在com.example.mqtt_designlayout_02.MainActivity $ BusyIndi​​cator.on preExecute(MainActivity.java:768)
 E / AndroidRuntime(20575):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
 E / AndroidRuntime(20575):在android.os.AsyncTask.execute(AsyncTask.java:535)
 E / AndroidRuntime(20575):在com.example.mqtt_designlayout_02.MainActivity.connectionIndicator(MainActivity.java:468)
 E / AndroidRuntime(20575):在com.example.mqtt_designlayout_02.MainActivity.MQTTConnect(MainActivity.java:460)
 E / AndroidRuntime(20575):在com.example.mqtt_designlayout_02.MainActivity.MQTT_Connection_Module(MainActivity.java:558)
 E / AndroidRuntime(20575):在com.example.mqtt_designlayout_02.MainActivity.access $ 0(MainActivity.java:548)
 E / AndroidRuntime(20575):在com.example.mqtt_designlayout_02.MainActivity $ 1.onClick(MainActivity.java:79)
 E / AndroidRuntime(20575):在android.view.View.performClick(View.java:4626)
 E / AndroidRuntime(20575):在android.view.View $ PerformClick.run(View.java:19293)
 E / AndroidRuntime(20575):在android.os.Handler.handleCallback(Handler.java:733)
 E / AndroidRuntime(20575):在android.os.Handler.dispatchMessage(Handler.java:95)
 E / AndroidRuntime(20575):在android.os.Looper.loop(Looper.java:157)
 E / AndroidRuntime(20575):在android.app.ActivityThread.main(ActivityThread.java:5293)
 E / AndroidRuntime(20575):在java.lang.reflect.Method.invokeNative(本机方法)
 E / AndroidRuntime(20575):在java.lang.reflect.Method.invoke(Method.java:515)
 E / AndroidRuntime(20575):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1265)
 E / AndroidRuntime(20575):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
 E / AndroidRuntime(20575):在dalvik.system.NativeStart.main(本机方法)

我很抱歉,但我从和取缔。所以您的解决方案就是,
更改以下行,
对话框=新的对话框(的getParent());


解决方案

看一看在这个博客帖子:的 http://possiblemobile.com/2013/06/context/ 和其中示出不同的上下文之间的比较向下​​滚动。

您正在使用的应用程序上下文:结果
对话框=新的对话框(getApplicationContext()); 结果
显示对话框,但是应用程序上下文无法做到这一点。

使用而不是活动场景。

更新参照您的评论:结果
您可以在活动实例传递给的AsyncTask 通过构造函数接受上下文参数。

例如:

 公共类MyAsyncTask扩展的AsyncTask&LT;&GT; {
    私人上下文的背景下;    公共MyAsyncTask(上下文的背景下){
        this.context =背景;
    }   //然后在preExecute使用环境
   //对话框=新的对话框(背景);
}

和你这样的活动打电话给你的AsyncTask

 新MyAsyncTask(本).execute();
//'这'是当前的活动实例。
//和活动扩展了上下文,因此,它是一个上下文。

in my App, i am trying to show a busy indicator while i am connecting to a server, so I created AsynchTask with dialogwith progressBar.

At run time when i try to connect to the server, the App crashs, out of the below posted LogCat it seems it is a problem with the dialog but i do not know what causes the error exactly.

the app is in LandScape mode with navigationdrawer

please have a look at the onPreExecute method and the logcat errors.

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)

i am sorry but i am banned from and. so your solution is just, Change following line, dialog = new Dialog(getParent());

解决方案

Have a look over this blog post: http://possiblemobile.com/2013/06/context/ , and scroll down where the comparison between different contexts is shown.

You are using application context:
dialog = new Dialog(getApplicationContext());
to show the dialog, but app context can't do that.

Use instead the activity context.

Update. With reference to your comment:
You can pass an activity instance to the AsyncTask by making your constructor accept a Context parameter.

Eg.:

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);
}

And you call your AsyncTask from the activity like this:

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

这篇关于在进度造成AsynchTask崩溃,&QUOT;无法添加窗口 - 令牌null不是一个申请表]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆