我试图将一个视图组类添加到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);
我做错了什么?以及如何解决这个问题?
答案 0 :(得分:1)
如果您在活动中获得WindowManager
,请直接使用getSystemService()
,请勿使用getApplicationContext
。