在非activity中使用alertDialog对话框
来源:互联网 发布:那些年网络神曲 编辑:程序博客网 时间:2024/05/21 22:46
有的时候,我们可能在activity类中调用非activity类中的方法,并需要在非activity类方法中定义AlertDialog对话框,怎么办呢:
1.一般情况是在activity类中定义
AlertDialog
,如下代码;
new AlertDialog.Builder(self) .setTitle("确认") .setMessage("确定吗?") .setPositiveButton("是", null) .setNegativeButton("否", null) .show();2.在非
activity类中定义AlertDialog:
DownLoadAPK.downLoad(getBaseContext());//activity类中
public static void downLoad(Context context){//非<font size="4"><code><font size="4"><font size="4"><code><font size="4"><span style="font-size:18px;"><code><span style="font-size:18px;"><span style="font-size:18px;"><code>activity类中</code></span></span></code></span></font></code></font></font></code></font>System.out.println("context : " + context);new AlertDialog.Builder(context) .setTitle("确认") .setMessage("确定吗?") .setPositiveButton("是", null) .setNegativeButton("否", null) .show();}这个时候会报错:Unable to add window -- token null is not for an application
因此我们必须写成如下形式才正确:
DownLoadAPK.downLoad(<span style="color:#009900;">GetCategoryApkList.this</span>);//<span style="font-size:18px;"><code></code></span>GetCategoryApkList<font size="4">为<font size="4"><code><font size="4"><font size="4"><code><font size="4"><span style="font-size:18px;"><code><span style="font-size:18px;"><span style="font-size:18px;"><code>activity</code></span></span></code></span></font></code></font></font></code></font>类名<code> </code></font><pre name="code" class="java"> public static void downLoad(Context context){//非activity类中System.out.println("context : " + context);new AlertDialog.Builder(context) .setTitle("确认") .setMessage("确定吗?") .setPositiveButton("是", null) .setNegativeButton("否", null) .show();}
0 0
- 在非activity中使用alertDialog对话框
- 解决在非Activity中使用startActivity
- 在非Activity中使用startActivity
- 解决在非Activity中使用startActivity
- android在非Activity中使用startActivity
- 解决在非Activity中使用startActivity
- 在非activity中使用startactivity
- 解决在非Activity中使用startActivity
- 解决在非Activity中使用startActivity
- 解决在非Activity中使用startActivity
- 解决在非Activity中使用startActivity
- 在非Activity类中使用findViewbyid
- 解决在非Activity中使用startActivity
- 解决在非Activity中使用startActivity
- Android中AlertDialog对话框的使用
- Android中AlertDialog(对话框)的使用
- Android Activity的setTitle,AlertDialog,Toast操作是否都必须在非主UI线程中操作呢?
- 在非模式对话框中使用acedcommand(),返回错误-5001
- Android 常用API使用 之 Base64
- Building Huuuuuge Apps with AngularJS
- java 并发编程(三)之synchronized
- mako语法
- Mac环境下svn命令行的使用
- 在非activity中使用alertDialog对话框
- 深入浅出Docker
- SAP EP实现下拉选择
- 解决vs2013性能分析工具报verfermon.exe 无法启动的错误
- gmail登陆提示密码不正确
- SEO知识分享
- 记录一下,博客访问量超过10万
- 自己写的点云和网格数据处理软件
- jquery 实现页面自动加载