当我尝试打开对话框时,出现以下 android 异常。
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)
我正在从 Service Android 调用对话框,并尝试了以下代码:
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();
}
}
});