简体   繁体   English

从另一个类作为主要活动调用时,Android Alertdialog崩溃并显示“无法添加窗口”异常

[英]Android Alertdialog crashes with “Unable to add window” exception, when calling it from another class as the main activity

I have a problem with an AlertDialog in Android. 我在Android中使用AlertDialog遇到问题。 In a class "Misc" there is a method which creates an alertdialog. 在“其他”类中,有一种创建警报对话框的方法。

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

When I call the method in the main activity by typing: 当我通过键入以下内容在主活动中调用该方法时:

Misc.getAlert(this)

it works, but in another class named Algorithm I call it in the same way: 它可以工作,但是在另一个名为Algorithm的类中,我以相同的方式调用它:

Misc.getAlert(context)

But then there is this 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)

I tried to set in getAlert 我试图在getAlert中设置

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

But then there is the error: "No enclosing instance of the type Sturzerkennung is accessible in scope" 但是然后出现错误:“作用域中无法访问Sturzerkennung类型的封闭实例”

context.getApplicationContext instead of Sturzerkennung.this didn't solve the problem either. context.getApplicationContext而不是Sturzerkennung.this也没有解决问题。 Has anybody an idea for this problem? 有人对这个问题有想法吗?

Problem solved.. The context in the Algorithm class was not from the Activity Sturzerkennung... 问题已解决。.算法类中的上下文并非来自Activity Sturzerkennung ...

In Sturzerkennung I put 我在Sturzerkennung

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

and then in Algorithm the parameter for getAlert is 然后在算法中,getAlert的参数为

Misc.getAlert(Sturzerkennung.context)

Now it works.. 现在可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在android主活动类中调用另一个类 - Calling another class in the Main activity class in android 从另一个活动中的按钮onClick调用另一个类中的AlertDialog - Calling an AlertDialog in another class from a button onClick in another Activity 调用另一个类时,Android无法启动活动componentinfo错误 - Android unable to start activity componentinfo error when calling another class android:App在自定义活动的Main活动中调用方法时崩溃 - android: App crashes when calling method in Main activity from custom activity 从另一个类调用主要活动方法 - Calling main activity method from another class Android:无法在主活动类中添加片段 - Android: Unable to add fragment in main activity class 从android活动调用时,OpenCv崩溃 - OpenCv crashes when calling from android activity 如何显示来自另一个类的alertdialog并在Current Activity(Android)中进行处理 - How to show alertdialog from another class and handle it in Current Activity(Android) 调用MediaController.show()时Android应用程序崩溃 - 无法添加窗口 - 令牌null无效 - Android app crashes when calling MediaController.show() - Unable to add window — token null is not valid 从Android中的其他活动(非主要活动)调用自定义ListView时出错? - Error calling a custom ListView from another activity (not main activity) in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM