从服务的Andr​​oid错误调用对话 [英] Error calling dialog from Service Android

查看:202
本文介绍了从服务的Andr​​oid错误调用对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到以下的android异常,当我尝试打开一个对话框。

  9月9日至20日:27:46.119:W / System.err的(558):$ android.view.WindowManager BadTokenException:无法添加窗口 - 令牌null不是一个应用
9月9日至20日:27:46.139:W / System.err的(558):在android.view.ViewRoot.setView(ViewRoot.java:440)
9月9日至20日:27:46.139:W / System.err的(558):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:181)
9月9日至20日:27:46.139:W / System.err的(558):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
9月9日至20日:27:46.139:W / System.err的(558):在android.app.Dialog.show(Dialog.java:269)
9月9日至20日:27:46.139:W / System.err的(558):在android.app.AlertDialog $ Builder.show(AlertDialog.java:907)

我是从一个服务调用Android的对话框,然后我尝试以下code:

  handler.post(新的Runnable(){
    公共无效的run(){
        尝试{
            新AlertDialog.Builder(getApplicationContext())。的setTitle(警报!)。setMessage(简单的信息!)。setPositiveButton(OK,新DialogInterface.OnClickListener(){
                公共无效的onClick(DialogInterface对话,诠释它){                }
            })。显示();
        }赶上(例外前){
            ex.printStackTrace();
        }
    }
});


解决方案

您不能打开从服务的对话框。 Dialog是一个UI组件,它需要与UI元素(活动)有关。你可以做的是开始从你的服务,看起来像对话框的活动。你可以给一个活动是DialogTheme的界面,因此它看起来像一个标准的Andr​​doid对话框。只要搜索计算器的对话活动的主题。

I get the following android exception when I try to open a dialog.

09-20 09:27:46.119: W/System.err(558): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
09-20 09:27:46.139: W/System.err(558):  at android.view.ViewRoot.setView(ViewRoot.java:440)
09-20 09:27:46.139: W/System.err(558):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:181)
09-20 09:27:46.139: W/System.err(558):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
09-20 09:27:46.139: W/System.err(558):  at android.app.Dialog.show(Dialog.java:269)
09-20 09:27:46.139: W/System.err(558):  at android.app.AlertDialog$Builder.show(AlertDialog.java:907)

I'm calling dialog from a Service Android, and I tried the following code:

handler.post(new Runnable() {
    public void run() {                               
        try{
            new AlertDialog.Builder(getApplicationContext()).setTitle("Alert!").setMessage("SIMPLE MESSAGE!").setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { 

                }
            }).show();
        } catch(Exception ex){
            ex.printStackTrace();
        }
    }
});

解决方案

You can't open a dialog from a service. Dialog is a UI component and it needs to be associated with a UI element (Activity). What you can do is to start an activity from your service that "looks like" a dialog. You can give the UI of an Activity a "DialogTheme" so that it looks like a standard Andrdoid dialog. Just search StackOverflow for "activity dialog theme".

这篇关于从服务的Andr​​oid错误调用对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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