如果添加了用于其按钮的setOnClickListeners,则PopUpWindow在Android中崩溃

时间:2017-10-10 19:51:26

标签: android popupwindow

显然,这与我在Internet上发现的其他PopUpWindows崩溃情况不同,因为我设法打开并显示PopUpWindow,但是当我为PopUpWindow包含的按钮添加代码时出现了问题。窗口的代码段是:

ImageButton speedImageButton;
LayoutInflater inflater = (LayoutInflater) LMMoviesMainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popvideocontrols, null); // popvideocontrols is a GridLayout
videocontrolsGloPopupWindow = new PopupWindow(layout, 800, 178, true);
videocontrolsGloPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
videocontrolsGloPopupWindow.setOutsideTouchable(false);
videocontrolsGloPopupWindow.setFocusable(false);
videocontrolsGloPopupWindow.showAtLocation(layout, Gravity.BOTTOM, 0, 37);
speedImageButton = (ImageButton)findViewById(R.id.speedImageButton);

如果我为按钮添加了一个点击监听器,它就会崩溃,例如:

speedImageButton.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {}
   });

它把这两种错误抛给了我:

java.lang.NullPointerException: Attempt to invoke virtual method 'void 
android.widget.SeekBar.setOnSeekBarChangeListener(android.widget.SeekBar$OnSeekBarChangeListener)' on a null object reference**

**BadTokenException: Unable to add window — token null is not valid

有什么想法吗?我应该尝试或修改什么?谢谢你的兴趣。

1 个答案:

答案 0 :(得分:0)

哇!感谢Mike M。:您的建议解决了我的问题。 我所要做的就是: 而不是:speedImageButton =(ImageButton)findViewById(R.id.speedImageButton);

关联popvideocontrols.xml的膨胀视图:

speedImageButton =(ImageButton)inflatedView.findViewById(R.id.speedImageButton);