监听PopupWindow上的控件出现nullpointerException异常,有Unknow Source
如下图所示
这里是有问题的代码
/*
* 获取popupwindow,并设置弹出位置
*/
View popupView = getLayoutInflater().inflate(R.layout.popup_window_layout, null);
// 设置背景为圆角
popupView.setBackgroundResource(R.layout.rounded_corners_view);
PopupWindow popupWindow = new PopupWindow(popupView, 200,200);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
Button button1 =(Button) findViewById(R.id.popupWindowCanclePoint); // “取消”键
Button button2 = (Button) findViewById(R.id.popupWindowRemovePoint);
Button button2 = (Button) popupView.findViewById(R.id.popupWindowRemovePoint);
PopupWindow popupWindow = new PopupWindow(popupView, 200,200);
和后面获取按钮Id的这几行代码
Button button1 =(Button) findViewById(R.id.popupWindowCanclePoint); // “取消”键
Button button2 = (Button) findViewById(R.id.popupWindowRemovePoint);
解决方法
1、这里设置弹出PopupWindow只是我这个项目中的一小部分,是在一个方法中设置。所以,要解决nullpointerException和nuknow Source 这两个问题,亦必须要不popupWindow设置为全局变量;
既将
PopupWindow popupWindow = new PopupWindow(popupView, 200,200);
变为
PopupWindow popupWindow;
````````````(省略中间代码)
popupWindow = new PopupWindow(popupView, 200,200);
2、将
Button button1 =(Button) findViewById(R.id.popupWindowCanclePoint); // “取消”键
Button button2 = (Button) findViewById(R.id.popupWindowRemovePoint);
变为
Button button1 =(Button) popupView.findViewById(R.id.popupWindowCanclePoint); // “取消”键
Button button2 = (Button) popupView.findViewById(R.id.popupWindowRemovePoint);
</pre><pre name="code" class="java">