1

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);
   }
Community
  • 1
  • 1

1 Answers1

6

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).

MarioB.
  • 2,356
  • 2
  • 17
  • 15