异常:android.view.WindowManager$BadTokenException: Unable to add window
2016-09-20 14:54
435 查看
原址:http://aijiawang-126-com.iteye.com/blog/662336
错误产生:
Java代码
private Context mcontext;
@Override
protected void onCreate(Bundle savedInstanceState) {mcontext = getApplicationContext();
System.out.println("mcontext=" + mcontext);
}
Java代码
d7a4
new AlertDialog.Builder(mcontext)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Warnning")
.setMessage(
"You forget to write the message. Do you want to fill out it ??")
.setPositiveButton("Yes", positiveListener).setNegativeButton(
"No", negativeListener).create().show();
导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApplicationContext()获得的Context,而必须使用Activity,因为只有一个Activity才能添加一个窗体。
解决方法:将new AlertDialog.Builder(Context context)中的参数用Activity.this(Activity是你的Activity的名称)来填充就可以正确的创建一个Dialog了。
Java代码
new AlertDialog.Builder(MyActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Warnning")
.setMessage(
"You forget to write the message. Do you want to fill out it ??")
.setPositiveButton("Yes", positiveListener).setNegativeButton(
"No", negativeListener).create().show();
错误产生:
Java代码
private Context mcontext;
@Override
protected void onCreate(Bundle savedInstanceState) {mcontext = getApplicationContext();
System.out.println("mcontext=" + mcontext);
}
Java代码
d7a4
new AlertDialog.Builder(mcontext)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Warnning")
.setMessage(
"You forget to write the message. Do you want to fill out it ??")
.setPositiveButton("Yes", positiveListener).setNegativeButton(
"No", negativeListener).create().show();
导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApplicationContext()获得的Context,而必须使用Activity,因为只有一个Activity才能添加一个窗体。
解决方法:将new AlertDialog.Builder(Context context)中的参数用Activity.this(Activity是你的Activity的名称)来填充就可以正确的创建一个Dialog了。
Java代码
new AlertDialog.Builder(MyActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Warnning")
.setMessage(
"You forget to write the message. Do you want to fill out it ??")
.setPositiveButton("Yes", positiveListener).setNegativeButton(
"No", negativeListener).create().show();
相关文章推荐
- android.view.WindowManager$BadTokenException: Unable to add window 异常的处理
- Android异常-android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.Bin
- Android实现浮窗时报的android.view.WindowManager$BadTokenException: Unable to add window异常
- Android 分析android.view.WindowManager$BadTokenException: Unable to add window异常
- Activity has leaked window that was originally added -界面退出时未关闭对话框异常 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? -
- 异常 android.view.WindowManager$BadTokenException: Unable to add window — token null
- android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window
- android.view.WindowManager$BadTokenException: Unable to add window -- token nul
- android.view.WindowManager$BadTokenException: Unable to add window -- token
- android.view.WindowManager$BadTokenException: Unable to add window
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
- android spinner点击android.view.WindowManager$BadTokenException: Unable to add window token is not va
- Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli