我的Activity正在尝试创建一个AlertDialog,它需要一个Context作为参数.如果我使用,这可以按预期工作:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Run Code Online (Sandbox Code Playgroud)
但是,我很谨慎地使用"this"作为上下文,因为当Activity被销毁并重新创建甚至在屏幕旋转之类的简单事件时,可能会发生内存泄漏.来自Android开发者博客上的相关帖子:
有两种简单的方法可以避免与上下文相关的内存泄漏.最明显的一个是避免在其自身范围之外逃避上下文.上面的例子显示了静态引用的情况,但内部类及其对外部类的隐式引用可能同样危险.第二种解决方案是使用Application上下文.只要您的应用程序处于活动状态并且不依赖于活动生命周期,此上下文就会存在.如果您计划保留需要上下文的长期对象,请记住应用程序对象.您可以通过调用Context.getApplicationContext()或Activity.getApplication()轻松获取它.
但是对于这AlertDialog()两者来说,getApplicationContext()或者getApplication()作为一个Context是可接受的,因为它抛出异常:
"无法添加窗口 - 令牌null不适用于应用程序"
那么,这真的应该被视为一个"错误",因为我们被正式建议使用Activity.getApplication(),但它没有宣传的功能吗?
吉姆
我正在尝试打开一个对话框窗口,但每次我尝试打开它都会抛出此异常:
Uncaught handler: thread main exiting due to uncaught exception
android.view.WindowManager$BadTokenException:
Unable to add window -- token null is not for an application
at android.view.ViewRoot.setView(ViewRoot.java:460)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:238)
at android.app.Activity.showDialog(Activity.java:2413)
Run Code Online (Sandbox Code Playgroud)
我是通过showDialog显示器的id 调用来创建的.该onCreateDialog处理器记录良好,我可以通过它没有问题的一步,但因为它看起来像我想的东西我已经把它贴吧:
@Override
public Dialog onCreateDialog(int id)
{
Dialog dialog;
Context appContext = this.getApplicationContext();
switch(id)
{
case RENAME_DIALOG_ID:
Log.i("Edit", "Creating rename dialog...");
dialog = new Dialog(appContext);
dialog.setContentView(R.layout.rename);
dialog.setTitle("Rename " + noteName);
break;
default:
dialog = null;
break;
}
return dialog;
}
Run Code Online (Sandbox Code Playgroud)
这有什么不足之处吗?有些问题在创建对话框时已经讨论过这个问题onCreate …
android runtimeexception android-dialog android-windowmanager
Iam 从预定服务中调用Asynctask每10分钟就会运行一次.
运行Service时,Progress对话框从OnpreExecute获取Exception .
错误:
FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:594)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
Run Code Online (Sandbox Code Playgroud)
编辑1: 警报管理器每5分钟调用一次服务
/*Alarm manager Service for From Server*/
private void setServerFetch() {
// for to Server to GPS PING
Intent myIntent1 = new Intent(LoginPage.this, AlarmService.class);
pendingintent1 = PendingIntent.getService(LoginPage.this, 1111, myIntent1, 0);
AlarmManager alarmManager5 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(System.currentTimeMillis());
calendar1.add(Calendar.SECOND, 1);
alarmManager5.set(AlarmManager.RTC_WAKEUP, …Run Code Online (Sandbox Code Playgroud) service android progressdialog android-asynctask android-pendingintent
我很难处理显示扩展a AlertDialog的Custom ListView类内部BaseAdapter.
AlertDialog.Builder alertbox = new AlertDialog.Builder(getParent().getApplicationContext());
alertbox.setMessage("No Internet Connection");
alertbox.setTitle("Warning");
alertbox.setIcon(R.drawable.trn_03);
alertbox.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
}
});
alertbox.show();
Run Code Online (Sandbox Code Playgroud)
以上是我正在使用的代码,LogCat错误是,
06-16 11:33:25.686: ERROR/AndroidRuntime(690): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Run Code Online (Sandbox Code Playgroud)
我认为问题是因为背景.我尝试了一些替代方案.但都没有效果.任何人都可以帮助我吗?
我读过很多像这样的问题,但似乎都没有解决我的问题.
问题在于:
AlertScreen ad = new AlertScreen(SensorListenerService.this);
Run Code Online (Sandbox Code Playgroud)
在我的服务类中:
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;
}
...
Run Code Online (Sandbox Code Playgroud)
它调用我的AlertScreen类:
public class AlertScreen extends AlertDialog {
public AlertScreen(Context context) {
super(context);
}
...
Run Code Online (Sandbox Code Playgroud)
LogCat有什么要说的:

谁能解释一下这个问题呢?