Kekis2014 Kekis2014 - 1 year ago 301
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(
at android.view.View.performClick(
at android.view.View$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(

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){


public int getCount() {
return mList.size();

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

public long getItemId(int position) {
return position;

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//use convertView recycle
holder=new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false);
holder.textView= (TextView) convertView.findViewById(;
holder.imageView= (ImageView) convertView.findViewById(;
holder.information= (Button) convertView.findViewById(;
holder = (ViewHolder) convertView.getTag();

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

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(, Gravity.CENTER, 0,0);

container.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
return true;


return convertView;

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


Answer Source

Instead of searching for in the information button view, search for in the convertView

Old code:

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

New code:

popUpWindow.showAtLocation(convertView.findViewById(, Gravity.CENTER, 0,0);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download