当从另一个类作为主要 Activity 调用它时,Android Alertdialog 崩溃并出现 "Unable to add window"异常

标签 android exception scope android-alertdialog android-context

我对 Android 中的 AlertDialog 有疑问。 在“Misc”类中,有一个创建警报对话框的方法。

public static void getAlert (Context context)
{
Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Notruf absenden?");
builder.setMessage("automatischer Notruf in 60 sec.");
builder.setCancelable(true);
builder.setPositiveButton("Ja", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               callEmergency(context);
           }
       });
builder.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
              countdown.cancel();
           }
       });

 final AlertDialog alertBox = builder.create();
 alertBox.show();

 countdown = new CountDownTimer(60000, 1000) 
 {
   public void onTick(long millisUntilFinished) 
   {
     alertBox.setMessage("automatischer Notruf in "+ (millisUntilFinished/1000) + " sec.");
   }
   public void onFinish()
   {
   alertBox.cancel();
   callEmergency(context);
   }
}.start();

当我通过键入调用主要 Activity 中的方法时:

Misc.getAlert(this)

它有效,但在另一个名为 Algorithm 的类中,我以相同的方式调用它:

Misc.getAlert(context)

但是有这个异常(exception):

09-16 17:15:32.304: E/AndroidRuntime(19797): FATAL EXCEPTION: main
09-16 17:15:32.304: E/AndroidRuntime(19797): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
09-16 17:15:32.304: E/AndroidRuntime(19797):    at android.view.ViewRoot.setView(ViewRoot.java:536)
09-16 17:15:32.304: E/AndroidRuntime(19797):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
09-16 17:15:32.304: E/AndroidRuntime(19797):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-16 17:15:32.304: E/AndroidRuntime(19797):    at android.app.Dialog.show(Dialog.java:241)
09-16 17:15:32.304: E/AndroidRuntime(19797):    at de.smehner.Sturzerkennung.Misc.getAlert(Misc.java:220)
09-16 17:15:32.304: E/AndroidRuntime(19797):    at de.smehner.Sturzerkennung.Algorithm.fall(Algorithm.java:203)
09-16 17:15:32.304: E/AndroidRuntime(19797):    at de.smehner.Sturzerkennung.Algorithm.performNonGraphical(Algorithm.java:68)
09-16 17:15:32.304: E/AndroidRuntime(19797):    at de.smehner.Sturzerkennung.SensorValueReceiver.onSensorChanged(SensorValueReceiver.java:66)
09-16 17:15:32.304: E/AndroidRuntime(19797):    at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:538)

我尝试在getAlert中设置

Builder builder = new AlertDialog.Builder(Sturzerkennung.this); // Sturzerkenung is the main activity

但随后出现错误:“在范围内无法访问类型 Sturzerkennung 的封闭实例”

context.getApplicationContext 而不是 Sturzerkennung.this 也没有解决问题。 有人知道这个问题吗?

最佳答案

问题解决了.. Algorithm 类中的上下文不是来自 Activity Sturzerkennung...

我在 Sturzerkennung 中放置

public static Context context;
...
context = this;

然后在 Algorithm 中,getAlert 的参数是

Misc.getAlert(Sturzerkennung.context)

现在可以了..

关于当从另一个类作为主要 Activity 调用它时,Android Alertdialog 崩溃并出现 "Unable to add window"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12448271/

相关文章:

java - Android App 防止被修改

java - Android 谷歌地图 API 无法正常工作

postgresql - Heroku - 无法通过 Play Framework 应用程序设置 postgres 数据库?

python-3.x - Python - 为什么需要声明非局部和全局变量?

c++ - 函数原型(prototype)作用域示例

Javascript - 全局变量和全局范围内的变量是否不同(在 jsfiddle 中)?

java - 如何将当前位置从一项 Activity 发送到另一项 Activity ?

android - 在 telephonyService.endCall() 之后隐藏传递的调用?

c++ - 修复返回问题。警告 : control reaches end of non-void function in C++

asp.net - ASPX 服务器异常