Android 为弹出窗口充气时出现NullPointerException
我正试图从列表项中的按钮(来自自定义列表适配器)放大弹出窗口,但在多个位置出现NullPointerException 下面是我试图使弹出窗口发生的按钮的点击:Android 为弹出窗口充气时出现NullPointerException,android,nullpointerexception,popupwindow,Android,Nullpointerexception,Popupwindow,我正试图从列表项中的按钮(来自自定义列表适配器)放大弹出窗口,但在多个位置出现NullPointerException 下面是我试图使弹出窗口发生的按钮的点击: public void onClick(View v) { LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); PopupWindow pw = null; i
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = null;
if (scores[0] == null) {
pw = new PopupWindow(inflater.inflate(R.layout.tag_table_row, null, false), 100, 100, true);
TextView tag = (TextView) v.findViewById(R.id.tag);
tag.setText("error!");
} else {
int x = tags.length;
int y = 5;
int z = Math.min(x, y);
for (int i = 0; i < z; i++) {
pw = new PopupWindow(inflater.inflate(R.layout.tag_table_row, null, false), 100, 100, true);
TextView tag = (TextView) v.findViewById(R.id.tag);
tag.setText(tags[i]);
int tag1score = Double.parseDouble(scores[i]);
TextView score = (TextView) v.findViewById(R.id.tagscore);
score.setText(Integer.toString(tag1score));
}
}
pw.showAtLocation(v.findViewById(R.id.fragment_content), Gravity.CENTER, 0, 0);
}
public void onClick(视图v){
LayoutInflater充气器=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u充气器\u SERVICE);
PopupWindow pw=null;
如果(分数[0]==null){
pw=新弹出窗口(充气器充气(R.layout.tag_table_row,null,false),100100,true);
TextView标记=(TextView)v.findViewById(R.id.tag);
tag.setText(“错误!”);
}否则{
int x=tags.length;
int y=5;
int z=数学最小值(x,y);
对于(int i=0;i
我在tag.setText(tags[I])上得到了NullPointerException代码>。如果我对此进行注释并尝试进行评分,我会在score.setText(Integer.toString(tag1score))处得到一个NullPointerException代码>。标记[]和分数[]都是在onClick之前初始化和填充的,我已经进行了测试,以确保两者中的结果都是字符串而不是null
关于为什么会发生这种NPE,您有什么想法吗?您需要使用正确的东西来查找视图。你正在膨胀到pw,但是你正在v中寻找你的观点。我认为这可能会更好
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = null;
PopupWindow pw = null;
if (scores[0] == null) {
popupView = layoutInflater.inflate(R.layout.tag_table_row, null, false);
pw = new PopupWindow(popupView, 100, 100, true);
TextView tag = (TextView) v.findViewById(R.id.tag);
tag.setText("error!");
} else {
int x = tags.length;
int y = 5;
int z = Math.min(x, y);
for (int i = 0; i < z; i++) {
popupView = layoutInflater.inflate(R.layout.tag_table_row, null, false);
pw = new PopupWindow(popupView, 100, 100, true);
TextView tag = (TextView) popupView.findViewById(R.id.tag);
tag.setText(tags[i]);
int tag1score = Double.parseDouble(scores[i]);
TextView score = (TextView) popupView.findViewById(R.id.tagscore);
score.setText(Integer.toString(tag1score));
}
}
pw.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
public void onClick(视图v){
LayoutInflater充气器=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u充气器\u SERVICE);
视图popupView=null;
PopupWindow pw=null;
如果(分数[0]==null){
popupView=LayoutFlater.inflate(R.layout.tag\u table\u行,null,false);
pw=新的PopupWindow(popupView,100100,true);
TextView标记=(TextView)v.findViewById(R.id.tag);
tag.setText(“错误!”);
}否则{
int x=tags.length;
int y=5;
int z=数学最小值(x,y);
对于(int i=0;i
它跟踪您膨胀到的视图,以便您可以在其中搜索您的元素。如果您只执行TextView标记=(TextView)findViewById(R.id.tag)代码>没有使用v
?我得到一个错误“类型new View.OnClickListener(){}的方法findViewById(int)未定义”,基本上您需要获得正确的上下文。尝试获取文本视图所在的窗口/活动的上下文。谢谢。你说的对我来说很有意义。但是popupwindow没有findViewById方法(或者类似的方法)。想法?请看答案的变化。您应该跟踪已膨胀到的视图。TextView分数=(TextView)pw.findViewById(R.id.tagscore)代码>应该是TextView分数=(TextView)popupView.findViewById(R.id.tagscore)
@user2163853,pw.showAtLocation(v.findViewById(R.id.fragment\u content),Gravity.CENTER,0,0)代码>将需要更改为pw.showAtLocation(弹出视图,重心,0,0)代码>。您可以使用另一个视图作为锚定点,但由于您使用的是重力.CENTER
和坐标(0,0),所以这并不重要popupView
就可以了。请重新编辑--参见上文。