第一次使用AlertDialog,当然首先得查阅android开发文档,见http://developer.android.com/reference/android/app/AlertDialog.html

            基本的使用方法就不在这赘述了,在使用过程中出现一个问题,就是在AlertDialog.Builder(Context context)的时候,这个context我是直接使用的getApplicationContext()来获取的,虽然编译没有错误,但是在运行的时候,程序会报错,logcat提示的错误是:

              android使用AlertDialog提示 Unable to add window -- token null is not for an application的解决方案_第1张图片

如上图红线标出的。

token null is not for an application,不是由application提供的。

导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApplicationContext()获得的Context,而必须使用Activity,因为只有一个Activity才能添加一个窗体。(参考自http://www.cnblogs.com/ok-lanyan/archive/2011/10/12/2208378.html)


那么我们直接改为   AlertDialog.Builder(MainActivity.this)就可以了



更多相关文章

  1. Android Studio xml android标签突然没有代码提示问题的解决
  2. Android导入工程提示Invalid project description
  3. Android中Failed to 。。。。。。timeout错误的解决方法
  4. 初次接触Android APP开发之安装Android studio软件出现的问题以
  5. 【学习Android遇到的错误】Android 开发遇到 java.lang.NoClassD
  6. android绑定服务运行错误:BinderProxy cannot be cast to ICounte

随机推荐

  1. 【转】Android:Bluetooth 的打开和关闭--
  2. android 模拟器启动报错:Failed to open
  3. 获取数字签名(.keystore)SHA1、MD5值
  4. Android(安卓)View体系(三)--实现 View 的
  5. Android(安卓)USB调试开关设置
  6. android播放网络或者本地视频(截取某帧做
  7. Android(安卓)Bitmap Api总结和使用方法
  8. android alertdialog布局 dialog布局
  9. Android(安卓)Intent的几种用法全面总结
  10. Android之GridView与ListView