user2163853
user2163853

Reputation: 917

NullPointerException when inflating a PopupWindow

I'm trying to inflate a PopupWindow from a button that sits within list items (from a custom list adapter) but am getting NullPointerExceptions in multiple places.

Here's the onClick for the button where I'm trying to make the PopupWindow happen:

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

I'm getting the NullPointerException at tag.setText(tags[i]);. If I comment that and just try to do scores, I get a NullPointerException at score.setText(Integer.toString(tag1score));. Both tags[] and scores[] are initialized and filled before the onClick, and I've tested to ensure that the results in both are Strings and NOT null.

Any thoughts as to why this NPE is happening?

Upvotes: 0

Views: 918

Answers (1)

HalR
HalR

Reputation: 11083

You need to use the right thing wherein to look for the view. You are inflating into pw, but you are looking in v for your views. I think this might work better;

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

it keeps track of the view you have inflated into, so you can search it for your elements.

Upvotes: 1

Related Questions