[英]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.