错误:无法添加窗口 - 令牌null不适用于应用程序

时间:2016-05-01 11:18:54

标签: android

我试图将一个视图组类添加到windowmanager但是会出现此错误

Unable to start activity ComponentInfo{com.example.amadey.final_version/com.example.amadey.final_version.LockActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

这是我的代码:

WindowManager manager = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
        WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
        localLayoutParams.width = 100;
        localLayoutParams.height = 100;
        localLayoutParams.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
        customViewGroup view = new customViewGroup(this);
        manager.addView(view, localLayoutParams);

我做错了什么?以及如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果您在活动中获得WindowManager,请直接使用getSystemService(),请勿使用getApplicationContext