创建AlertDialog是报错的原因
来源:互联网 发布:learnx mac 编辑:程序博客网 时间:2024/05/20 23:32
代码如下:
Builder builder = new Builder(getApplicationContext());
builder.setItems(actionItems, new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),which , Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
报错:
Unable to add window -- token null is not for an application
原因是因为使用了getApplicationContext(),应该声明一个全局变量在onCreate()方法中将Activity.this赋值给全局变量然后使用。
修正后的代码如下:
Builder builder = new Builder(mContext);
builder.setItems(actionItems, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext,which , Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
0 0
- 创建AlertDialog是报错的原因
- AlertDialog如何创建layerDim的过程
- Android_创建拥有自己adapter的Alertdialog
- AlertDialog的使用(二):分别创建
- 使用AlertDialog创建对话框的大致步骤
- 自定义AlertDialog的创建和使用
- 对话框AlertDialog的基本类型与创建
- AlertDialog创建6种对话框的用法
- AlertDialog:警告对话框的创建方法
- AlertDialog的六种创建方式
- alertdialog创建对话框的几个小点
- alertdialog的六种创建方法
- Android之使用AlertDialog类和AlertDialog.Builder类创建带取消,确定,中立的对话框
- BroadcastReceiver中创建alertdialog
- AlertDialog 快速创建
- 使用AlertDialog创建对话框
- 使用AlertDialog创建对话框
- Android创建AlertDialog对话框
- 正则表达式30分钟入门教程
- 让微信二维码扫描您的APK
- LINQ查询概述
- 关于hibernate的缓存使用
- 全自动软化水设备:全自动软化水设备设计方案
- 创建AlertDialog是报错的原因
- oracle建表的时候同时创建主键,外键,注释,约束,索引
- jquery实现ajax功能和读取xml
- Ubuntu14.04下安装Hadoop2.4.0 --- 伪分布模式
- C#时间/日期格式大全
- hibernate 延迟加载
- 一个jq投票,无论成功与否3秒后自动关闭提示框
- iOS七牛云存储
- arcgis基础(2)