使用popwindow中又碰到一个白痴问题,在此留作纪念,希望对大家有帮助

popupwindow之所以叫这个名字,肯定是要从某个地方弹出啦,但是从哪个地方呢?必须是指定一个view嘛

void android.widget.PopupWindow.showAtLocation(View parent, int gravity, int x, int y)

调用这个函数就能显示popupwindow了,但是有的同学会有幸碰到一个异常,关于这个异常的解释是unable to add window -- token null is not valid;is your activity running?

看起来就有点纳闷了,为啥呢?肯定是你showAtLocation第一个参数用不鸟啦,你有可能是在activity之外showAtLocation,或者是在Activity的onCreate()函数里面调用了showAtLocation,多半是后者,由于你的popupwindow要依附于一个activity,而activity的onCreate()还没执行完,哪来的popup让你弹出来嘛。

因此,你要做的就是让这个showAtLocation的调用再晚一点,下面网上找个一个解决方案,我把我的代码直接贴吧,还是代码可靠,神马都是bullshit!在activity类中添加handler

	private Handler popupHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
popupWindow.showAtLocation(findViewById(R.id.rlShowImage), Gravity.CENTER|Gravity.CENTER, 0, 0);
popupWindow.update();
break;
}
} };

然后再在onCreate()函数中添加

popupHandler.sendEmptyMessageDelayed(0, 1000);

也就是增加一个延时的任务而已啦,简单有效。

android AlertDialog显示错误 Unable to add window token null is not for an application解决办法


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的名称

popupwindow使用之异常:unable to add window -- token null is not valid的更多相关文章

  1. Activity has leaked window that was originally added -界面退出时未关闭对话框异常 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? -

    退出Activity时弹出登录框,点击确定finish当前Activity,结果报了这个错,随后查找资料知道 原因: 是因为退出Activity时没有关闭弹出框,出现了这个错误 解决方法: 只需要在a ...

  2. android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

      原博客地址:http://aijiawang-126-com.javaeye.com/blog/662336 在Activity中newSpinner是我把mContext传入,但是出了 andr ...

  3. android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic

    之前遇到过这样的问题, 04-12 10:40:33.302: E/AndroidRuntime(17213): Caused by: android.view.WindowManager$BadTo ...

  4. Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

    在广播中启动一个Dialog时出现如下错误信息:Caused by: android.view.WindowManager$BadTokenException: Unable to add windo ...

  5. Unable to add window -- token null is not for an application错误的解决方法 android开发

    Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not f ...

  6. Android自用-----WindowManager$BadTokenException: Unable to add window -- token null is not for an application

    转自http://www.cnblogs.com/oakpip/archive/2011/04/06/2007310.html 错误产生: private Context mcontext; @Ove ...

  7. Unable to add window -- token null is not for an application

    导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApp ...

  8. 自定义对话框 提示:Unable to add window token null is not for an application

    这是因为在new Dialog(context);的时候传入的context是通过getApplicationContext()获得的,这样就会报错. 把context的获得方式改为MainActiv ...

  9. bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token

    ========4       关于android的一个常见错误:Unable to add window --token is not valid android.view.WindowManage ...

随机推荐

  1. idea设置单行注释格式(包括配置文件)

    idea中文件单行注释默认在行首位置 修改方法:进入File-->Settings-->Editor-->Code Style-->Java,修改Code Generation ...

  2. socket实现简单的FTP

    一.开发环境 server端:centos 7  python-3.6.2 客户端:Windows 7 python-3.6.2 pycharm-2018 程序目的:1.学习使用socketserve ...

  3. CSS - 块状元素、内联元素和内联块状元素

    在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素(block).内联元素(又叫行内元素,inline)和内联块状元素(inline-block). 1. 常用的块状元素有: < ...

  4. 安装完 Ubuntu 16.04.1,重启出现[sda] Assuming drive cache: write through的问题

    重装了一下ubuntu,安装成功后重启出现了这个问题 刚开始以为是重启慢,就没在意这么多,可是我等了半个小时,(我特么的真闲,其实是忙别的忘了),还不行,咦,然后我就去找了找问题,哈哈哈哈 看图说话, ...

  5. Codeforces Round #606 (Div. 2) - E. Two Fairs(割点+dfs)

    题意:给你一张无向连通图,对于求有多少对$(x,y)$满足互相到达必须经过$(a,b)$,其中$x\neq a,x\neq b,y\neq a,y\neq b$ 思路:显然$a,b$都必须为割点,所以 ...

  6. DNS域名解析服务(重点)

    一 .DNS  系统的作用 1.DNS 服务器概述 DNS 系统在网络中的作用就是维护着一个地址数据库,其中记录了各种主机域名:与 IP地址的对应关系,以便为客户程序提供正向或反向的地址查询服务,即正 ...

  7. MRCP接口MRCPRecog 简介

    功能:开始一个语音识别,一边讲话,一边识别,需要ASR服务器. 原型:MRCPRecog (grammar, options) grammar ---- 语法文件,可以是一个xml文件 options ...

  8. mysql事务隔离分析

    首先说明下,这里主要内容为整理总结网络搜索的零散信息. 写在最前面,mysql事务是在Innodb引擎中得以实现的,如果这点不了解的话,请自行了解. 事务直接数据的可见性通过MVCC(多版本并发控制) ...

  9. Python 爬取 热词并进行分类数据分析-[热词分类+目录生成]

    日期:2020.02.04 博客期:143 星期二   [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[ ...

  10. where、having区别

    where  <<   group by   <<   having where筛选是在分组之前筛选,筛选完之后再group by having是分组之后再筛选,筛选完之前先g ...