Kekis2014 Kekis2014 - 4 months ago 55
Android Question

Null Pointer Exception When Attempting to create a pop up window in a listView with custom adapter

Im attempting to make a pop up window after I click a button in a listView, and then close it by clicking outside of the box. However, I am getting this error when I attempt to click the button.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.IBinder android.view.View.getWindowToken()' on a null object reference
at android.widget.PopupWindow.showAtLocation(PopupWindow.java:897)
at logistica.enviaflores.com.logistica.utilities.MyAdapter$1.onClick(MyAdapter.java:81)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)


I can't exactly understand what I am doing wrong for it to throw a null pointer exception. Perhaps i'm coding it incorrectly, but it seems right to me even after I go back and look at it. Any help would be greatly appreciated.

public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<Bean> mList;
private PopupWindow popUpWindow;
private LayoutInflater inflater;

public MyAdapter(Context context,List<Bean> list){
mContext=context;
mList=list;


}

@Override
public int getCount() {
return mList.size();
}

@Override
public Object getItem(int position) {
return mList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//use convertView recycle
if(convertView==null){
holder=new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false);
holder.textView= (TextView) convertView.findViewById(R.id.textView2);
holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2);
holder.information= (Button) convertView.findViewById(R.id.button5);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}

//set text and url
holder.information.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {


inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup container = (ViewGroup) inflater.inflate(R.layout.information_popup, null);
popUpWindow = new PopupWindow(container, 400,400,true);
//***I believe the problem happens right in the line under this sentence!***
popUpWindow.showAtLocation(v.findViewById(R.id.orders), Gravity.CENTER, 0,0);

container.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
popUpWindow.dismiss();
return true;
}
});
}
});

holder.textView.setText(mList.get(position).getText());
Picasso.with(mContext).load(mList.get(position).getUrl()).resize(500,500).into(holder.imageView);

return convertView;
}

class ViewHolder{
TextView textView;
ImageView imageView;
Button information;
Button close;

}
}

Answer

Instead of searching for R.id.orders in the information button view, search for in the convertView

Old code:

popUpWindow.showAtLocation(v.findViewById(R.id.orders), Gravity.CENTER, 0,0);

New code:

popUpWindow.showAtLocation(convertView.findViewById(R.id.orders), Gravity.CENTER, 0,0);
Comments