我根據網上教程定義一個定製的浮動視圖組件,但問題是,每次我打電話removeView(),有一個例外,所有其他部分工作正常,以下是的onclick監聽器。removeView(MVIEW)拋出一個拋出:IllegalArgumentException
followButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView focusView = new ImageView(getActivity());
focusView.setImageResource(R.drawable.follow_me);
WindowManager.LayoutParams focusParam = new WindowManager.LayoutParams();
focusParam.type=2002;
focusParam.format=1;
focusParam.flags=40; //default(32)+ FLAG_NOT_FOCUSABLE(8)
focusParam.width=30;
focusParam.height=30;
focusParam.x=0;focusParam.y=0;
focusParam.setTitle("Load Average");
WindowManager wm1 = (WindowManager)getActivity().getSystemService(Context.WINDOW_SERVICE);
followModeOn=!followModeOn;
if (followModeOn) {
wm1.addView(focusView, focusParam);
}
else {
wm1.removeView(focusView);
}
}
});
以下是錯誤報告:
01-23 00:06:39.163: E/AndroidRuntime(31651): java.lang.IllegalArgumentException: View=android.widget.ImageView{46586dc0 V.ED.... ......I. 0,0-0,0} not attached to window manager
01-23 00:06:39.163: E/AndroidRuntime(31651): at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:370)
01-23 00:06:39.163: E/AndroidRuntime(31651): at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:299)
01-23 00:06:39.163: E/AndroidRuntime(31651): at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
01-23 00:06:39.163: E/AndroidRuntime(31651): at study.runtracker.map.OSMFragment$4.onClick(OSMFragment.java:180)
01-23 00:06:39.163: E/AndroidRuntime(31651): at android.view.View.performClick(View.java:4438)
01-23 00:06:39.163: E/AndroidRuntime(31651): at android.view.View$PerformClick.run(View.java:18422)
01-23 00:06:39.163: E/AndroidRuntime(31651): at android.os.Handler.handleCallback(Handler.java:733)
01-23 00:06:39.163: E/AndroidRuntime(31651): at android.os.Handler.dispatchMessage(Handler.java:95)
01-23 00:06:39.163: E/AndroidRuntime(31651): at android.os.Looper.loop(Looper.java:136)
01-23 00:06:39.163: E/AndroidRuntime(31651): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-23 00:06:39.163: E/AndroidRuntime(31651): at
java.lang.reflect.Method.invokeNative(Native Method)
01-23 00:06:39.163: E/AndroidRuntime(31651): at java.lang.reflect.Method.invoke(Method.java:515)
01-23 00:06:39.163: E/AndroidRuntime(31651): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-23 00:06:39.163: E/AndroidRuntime(31651): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-23 00:06:39.163: E/AndroidRuntime(31651): at dalvik.system.NativeStart.main(Native Method)