【问题标题】:java.lang.NullPointerException:at android.widget.PopupWindow.setContentView when trying to inflate Layoutjava.lang.NullPointerException:在 android.widget.PopupWindow.setContentView 尝试膨胀布局时
【发布时间】:2018-07-03 23:54:02
【问题描述】:

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

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

【问题讨论】:

    标签: android android-inflate


    【解决方案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...
        }
    }
    

    【讨论】:

      【解决方案2】:

      我的 2 美分解决方案。

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多