【发布时间】:2013-06-11 22:36:32
【问题描述】:
我在崩溃报告中收到此错误android.view.WindowManager$BadTokenException。在某些设备上它只报告异常但不会使应用程序崩溃,其他设备会遇到崩溃。
这与应用程序如何显示对话框有关。
其他答案表明使用了错误的context,就像一个全局的,但在我的情况下,我没有这样做,我将我的活动的上下文传递给另一个对象的方法。
public class Utils {
包含一个方法
public static void noConnection(Context context){
final CustomAlertDialog alert = new CustomAlertDialog(context, context.getString(R.string.ErrorPastTense), context.getString(R.string.ErrorInternet), context.getString(R.string.OkButton), null);
View.OnClickListener listener = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int id = v.getId();
switch(id){
case R.id.alertConfirm:
alert.dismiss();
break;
default:
break;
}
}
};
alert.setListener(listener);
alert.show();
}
由我的活动中的方法调用,例如Utils.noConnection(myActivity.this);
错误日志显示异常发生在alert.show()
为什么?以及如何避免
【问题讨论】:
-
在 onCreate() Utils.noConnection(context); 中使用 Context context=null 作为全局和 context=this;
标签: android android-activity android-context android-windowmanager