Android: unable to add window - token null is not for an application [duplicate]
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开发者_Go百科 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
).
精彩评论