安卓:无法添加窗口 - 令牌null不是一个应用程序 [英] Android: unable to add window - token null is not for an application

查看:135
本文介绍了安卓:无法添加窗口 - 令牌null不是一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window">Android 1.6:android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不是一个应用程序

  

android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不是一个应用程序   在android.view.ViewRoot.setView(ViewRoot.java:509)   在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)   在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)

我的code:

 公共AAAA级扩展视图{
  私人窗口管理mWindowManager;
 公共AAAA(上下文的背景下){
  超(上下文);
 }

@覆盖
保护无效的OnDraw(帆布油画){
  canvas.drawBitmap(BitmapFactory.de codeResource(this.getResources(),R.drawable.icon),0,0,NULL);
  super.onDraw(画布);
 }

 @覆盖
 公共布尔的onTouchEvent(MotionEvent事件){
  DDD();
  返回super.onTouchEvent(事件);
 }

 无效DDD(){
     mWindowManager = WindowManagerImpl.getDefault();

        WindowManager.LayoutParams LP =新的LayoutParams(50,50);
        lp.x = 50;
        lp.y = 50;

        mWindowManager.addView(这一点,LP);
   }
 

解决方案

这异常通常,如果您使用无效的情况下抛出。什么是你传递给你的AAA类的构造函数?如果你通过 getApplicationContext()则可能会失败。尝试过它负责显示您的观点活动的情况下(通常与基准)。

Possible Duplicate:
Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application"

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application at android.view.ViewRoot.setView(ViewRoot.java:509) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)

my code:

    public class aaaa extends View {
  private WindowManager mWindowManager;
 public aaaa(Context context) {
  super(context);
 }

@Override
protected void onDraw(Canvas canvas) {
  canvas.drawBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.icon), 0, 0, null);
  super.onDraw(canvas);
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  ddd();
  return super.onTouchEvent(event);
 }

 void ddd(){
     mWindowManager = WindowManagerImpl.getDefault();

        WindowManager.LayoutParams lp = new LayoutParams(50,50);
        lp.x = 50;
        lp.y = 50;

        mWindowManager.addView(this, lp);
   }

解决方案

This exception is usually thrown if you work with an invalid context. What are you passing to the Constructor of your aaa class? If you pass getApplicationContext() it may fail. Try passing the context of the activity which is responsible for displaying your view (usually with the reference this).

这篇关于安卓:无法添加窗口 - 令牌null不是一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆