【问题标题】:Unable to add window -- token null is not for an application from Service无法添加窗口 - 令牌 null 不适用于服务中的应用程序
【发布时间】:2014-05-07 11:34:23
【问题描述】:

我读过很多这样的问题,但似乎没有一个能解决我的问题。

问题出在:

 AlertScreen ad = new AlertScreen(SensorListenerService.this);

在我的服务类中:

public class SensorListener extends Service implements SensorEventListener {

public int onStartCommand(Intent intent, int flags, int startId) {
    startForeground(Process.myPid(), new Notification());

    AlertScreen ad = new AlertScreen(SensorListener.this); //problem
    ad.show();
    
    return START_STICKY;
}
...

它调用我的 AlertScreen 类:

public class AlertScreen extends AlertDialog {

public AlertScreen(Context context) {
    super(context);
}
...

LogCat 有什么要说的:

谁能解释一下这个问题?

【问题讨论】:

  • 在您的服务的onCreate() 上试试这个AlertScreen ad = new AlertScreen(this);
  • @SimplePlan 不,同样的故事。
  • 我的回答成功了吗?

标签: android android-alertdialog


【解决方案1】:

您不能从服务上下文显示对话框。

我建议您打开一个实际显示对话框的 Activity,或查看此答案以了解如何显示系统警报。

https://stackoverflow.com/a/19269931/1725088

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 2019-01-04
    • 2011-12-17
    • 1970-01-01
    • 2011-02-07
    相关资源
    最近更新 更多