忘记从哪里转来的,暂时不标注吧.
错误问题提示:android.view.WindowManager$BadTokenException: Unable to add window — token null
博客分类: 平常遇到的问题与解决方法错误产生:
private Context mcontext;
@Override
protected void onCreate(Bundle savedInstanceState) {mcontext = getApplicationContext();
System.out.println("mcontext=" + mcontext);
}
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(Context context)中的参数用Activity.this(Activity是你的Activity的名称)来填充就可以正确的创建一个Dialog了。
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();