android AlertDialog显示错误 Unable to add window token null is not for an application解决办法
2012-08-23 11:47
716 查看
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder(getApplicationContext())
.setTitle(R.string.dialogTitle)
.setMessage(R.string.dialogMessage)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "你按了确定。", Toast.LENGTH_SHORT).show();
}
}).show();
}
});
}
报错:Unable to add window token null is not for an application
异常原因:AlertDialog创建语句public AlertDialog.Builder (Context context)中,不能使用getApplicationContext()得到的context,而必须使用Activity,所以解决如下
解决办法:
将 new AlertDialog.Builder(getApplicationContext()) 改为 new AlertDialog.Builder(MainActivity.this)
其中MainActivity 为当前Activity的名称
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder(getApplicationContext())
.setTitle(R.string.dialogTitle)
.setMessage(R.string.dialogMessage)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "你按了确定。", Toast.LENGTH_SHORT).show();
}
}).show();
}
});
}
报错:Unable to add window token null is not for an application
异常原因:AlertDialog创建语句public AlertDialog.Builder (Context context)中,不能使用getApplicationContext()得到的context,而必须使用Activity,所以解决如下
解决办法:
将 new AlertDialog.Builder(getApplicationContext()) 改为 new AlertDialog.Builder(MainActivity.this)
其中MainActivity 为当前Activity的名称
相关文章推荐
- android AlertDialog显示错误 Unable to add window token null is not for an application
- AlertDialog显示错误 Unable to add window token null is not for an application
- AlertDialog显示错误 Unable to add window token null is not for an application
- AlertDialog显示错误 Unable to add window token null is not for an application
- AlertDialog显示错误 Unable to add window token null is not for an application
- AlertDialog显示错误 Unable to add window token null is not for an application
- AlertDialog显示错误 Unable to add window token null is not for an application
- AlertDialog显示错误 Unable to add window token null is not for an application
- android使用AlertDialog提示 Unable to add window -- token null is not for an application的解决方案
- Android酱油笔记之关于AlertDialog的Unable to add window token null is not for an application解决办法
- Dialog Unable to add window -- token null is not for an application错误的解决方法
- 使用AlertDialog.Builder的时候出现错误:unable to add window --token null is not for an application的解决办法
- Unable to add window -- token null is not for an application错误的解决方法 android开发
- Unable to add window -- token null is not for an application错误的解决方法 android开发
- Unable to add window -- token null is not for an application错误的解决方法
- Unable to add window -- token null is not for an application错误的解决方法
- AlertDialog显示错误 Unable to add window token null is not for an application
- Unable to add window -- token null is not for an application错误的解决方法
- 在创建一个Dialog时,发生的错误: Unable to add window -- token null is not for an application
- AlertDialog:Unable to add window -- token null is not for an application