2014-01-22 242 views
0

我根據網上教程定義一個定製的浮動視圖組件,但問題是,每次我打電話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) 

回答

0

這個斜面工作。您每次收到點擊時都會創建一個新的ImageView,因此您需要保留一個參考資料,以便首先創建該參考資料。

當你在每次重新創建新認爲不能從視圖,因爲它從來沒有被添加到「父」視圖中刪除。

0

作爲日誌貓說:focusView尚未連接到窗口管理器。您正在創建一個全新的ImageView,然後在附加它之前嘗試將其刪除。

我會嘗試保持一個參考focusView以便以後可以將其刪除。

View mFocusView = null; 

followButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // Create the focus view, if not already done 
     if (mFocusView == null) 
      mFocusView = new ImageView(getActivity()); 

     mFocusView.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; 
     // This will work as long as followModeOn is false here, the first pass through. 
     if (followModeOn) { 
      wm1.addView(mFocusView, focusParam); 
     } 
     else { 
      wm1.removeView(mFocusView); 
     } 
    } 
}); 
0

感謝您的建議,現在我將一個構造函數移出onclick並解決問題。

 ImageView focusView = new ImageView(getActivity()); 
     @Override 
     public void onClick(View v) {    
      focusView.setImageResource(R.drawable.follow_me); 
相關問題