0

单击图像按钮后,我试图显示一个弹出窗口。我为弹出窗口创建了一个额外的布局,并使用 LayoutInflater 从布局中创建了一个视图。我正在使用“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);
    }

由于 inflate 函数返回 null,我得到以下 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)

我不确定我哪里出错了。请帮忙

4

3 回答 3

1

对于将来可能会出现此错误的其他人,我在sourceCode上发现有一个带有 PopupWindow(View contentView) 的构造函数并被调用。

所以我找到了一个解决方法:我不是直接调用构造函数,而是创建一个返回该视图实例的静态方法。它首先创建 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...
    }
}
于 2013-09-13T09:56:15.583 回答
1

我的 2 美分解决方案。

如果您已经覆盖了构造函数 MyPopupWindow(Context context) 并且仍然得到 NullPointerException - 请检查您是否在第一行调用 super(context),因为您可能没有)。

于 2013-11-11T14:21:05.520 回答
0

找到了解决该问题的方法。错误在于代码行

PopupWindow popupWindow = new PopupWindow(); 

这是隐式调用 setContentView 方法。下面的代码在我使用另一个版本的带有 View、height 和 width 参数的 PopupWindow 构造函数时工作正常。

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);

    }
于 2013-05-29T17:18:14.133 回答