2011-05-12 53 views
0

我創建了彈出窗口不能排除popUpWindow

public void myPopUp() 
    { 

     LinearLayout myView = new LinearLayout(getApplicationContext()); 
     pw = new PopupWindow(getApplicationContext()); 
     pw.setContentView(myView); 
     myView.setBackgroundColor(Color.WHITE); 
     pw.setWidth(curWidth-100); 
     pw.setHeight(curHeight-100); 
     pw.showAtLocation(background, Gravity.CENTER, 0, 0); 
    } 

和overrided後退按鈕按下dissmis

public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      getNewWH(); 
       middle.removeView(bb); 
      if(statusBG==2) 
      { 
       log("statusBG = "+statusBG); 
       showFeed(); 
       statusBG=1; 
      } 
      else 
      if(statusBG==1) 
       { 
        finish(); 
       } 
      else 
      if (pw.isShowing()) { 
       log("here again"); 
       pw.dismiss(); 
       log("here"); 

      } 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

爲u能理解我 - 我想dissmis /隱藏popupwindow當我按下後退按鈕但現在我得到如下因素錯誤的:(

05-12 12:27:15.373: ERROR/WindowManager(1259): Activity myClass.com.myClass has leaked window [email protected] that was originally added here 
05-12 12:27:15.373: ERROR/WindowManager(1259): android.view.WindowLeaked: Activity myClass.com.myClass has leaked window [email protected] that was originally added here 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at android.view.ViewRoot.<init>(ViewRoot.java:247) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at android.widget.PopupWindow.invokePopup(PopupWindow.java:828) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at android.widget.PopupWindow.showAtLocation(PopupWindow.java:688) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at myClass.com.myClass.myPopUp(myClass.java:743) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at myClass.com.myClass$15.onClick(myClass.java:724) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at android.view.View.performClick(View.java:2408) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at android.view.View$PerformClick.run(View.java:8816) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at android.os.Handler.handleCallback(Handler.java:587) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at android.os.Handler.dispatchMessage(Handler.java:92) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at android.os.Looper.loop(Looper.java:123) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at java.lang.reflect.Method.invoke(Method.java:521) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
05-12 12:27:15.373: ERROR/WindowManager(1259):  at dalvik.system.NativeStart.main(Native Method) 

有人能幫助我,告訴如何糾正dismis彈出w ^母雞我按backButton?

p.s.對不起我的英語不好,希望你能理解我。問候,彼得。

+0

使用對話框可以做同樣的事情,試試吧,很容易管理 – 2011-05-13 03:50:02

+0

我在這裏找到了解決方案 - > http://stackoverflow.com/questions/3121232/android-popup-window-dismissal – Peter 2011-05-13 09:43:35

回答

0

不要使用getApplicationContext()來傳遞上下文或this。

LinearLayout myView = new LinearLayout(context or this);

+0

爲什麼?更多的話) – Peter 2011-05-12 12:56:26

+0

使用對話框可以做同樣的事情,試試吧,它很容易管理 – 2011-05-13 03:49:39

+0

不,謝謝,我需要popUp :( – Peter 2011-05-13 07:32:03

2

對於解散PopupWIndow而不是根後臺活動或視圖來設置焦點。 popupWindow.setFocusable(true);

彈出窗口沒有焦點,所以當按下後面時,它會關閉主要活動,因爲焦點位於主要活動上。