2013-05-29 143 views
0

我想單擊圖像按鈕後顯示彈出窗口。我爲彈出窗口創建了一個額外的佈局,並使用LayoutInflater從佈局創建視圖。我使用「的setContentView」java.lang.NullPointerException:在android.widget.PopupWindow.setContentView當試圖膨脹佈局

PopupWindow popupWindow = new PopupWindow(); 
LayoutInflater popupLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View popupWindowView = null; 

     try{ 
      popupWindowView = popupLayoutInflater.inflate(R.layout.popuplayout, null); 
     } 
     catch(InflateException e){ 
      System.out.println(e.getMessage()); 
     } 

     if(popupWindowView!=null) 
      popupWindow.setContentView(popupWindowView); 
     popupWindow.showAtLocation(parentView, android.view.Gravity.NO_GRAVITY, 10, 10); 
    } 

我得到以下NullPointerException異常的充氣函數返回一個空設置彈出窗口這個觀點。

05-29 00:20:08.582: W/dalvikvm(304): threadid=1: thread exiting with uncaught exception  (group=0x4001d800) 
05-29 00:20:08.592: E/AndroidRuntime(304): FATAL EXCEPTION: main 
05-29 00:20:08.592: E/AndroidRuntime(304): java.lang.NullPointerException 
05-29 00:20:08.592: E/AndroidRuntime(304): at  android.widget.PopupWindow.setContentView(PopupWindow.java:377) 
05-29 00:20:08.592: E/AndroidRuntime(304): at android.widget.PopupWindow.<init>(PopupWindow.java:279) 
05-29 00:20:08.592: E/AndroidRuntime(304): at android.widget.PopupWindow.<init>(PopupWindow.java:259) 
05-29 00:20:08.592: E/AndroidRuntime(304): at android.widget.PopupWindow.<init>(PopupWindow.java:216) 

我不知道我要去的地方錯了。請幫忙

回答

0

找到了解決方法。錯誤發生在代碼行

PopupWindow popupWindow = new PopupWindow(); 

這隱式地調用了setContentView方法。下面的代碼在我使用另一個版本的PopupWindow構造函數和View,高度和寬度參數的情況下工作正常。

public void onClick(View arg0) { 

     LayoutInflater popupLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View popupWindowView = null; 
     PopupWindow popupWindow = null; 
     try{ 
      popupWindowView = popupLayoutInflater.inflate(R.layout.popuplayout, null); 
     } 
     catch(InflateException e){ 
      System.out.println(e.getMessage()); 
     } 


     if(popupWindowView!=null) 
     { 
      popupWindow = new PopupWindow(popupWindowView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     } 
     if(popupWindow!=null) 
      popupWindow.showAtLocation(parentView, android.view.Gravity.NO_GRAVITY, 100, 100); 

    } 
1

對於其他人誰可能在未來的這個錯誤,我在sourceCode發現有與PopupWindow(查看內容查看)構造函數,它被稱爲。

所以我找到了一個解決方法:不是直接調用構造函數,而是創建一個返回此視圖的實例的靜態方法。它首先創建contentView,傳遞給構造函數,以便它可以直接調用super(contentView)構造函數。就是這樣。

public class MenuPopup extends PopupWindow { 

    public static MenuPopup getInstance(Activity act) { 
     LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     contentView = inflater.inflate(R.layout.menu, null); 
     IvrMenuNew popup = new IvrMenuNew(act, contentView); 
     return popup; 
    } 

    public MenuPopup (Activity act, View contentView) { 
     super(contentView); 
     // TODO: whatever else you need to do... 
    } 
} 
1

我2美分的解決方案。

如果你有重寫的構造函數MyPopupWindow(Context context)並且仍然得到NullPointerException - 檢查你是否在第一行調用super(context),因爲你可能沒有)。