尝试在带有自定义适配器的列表视图中创建弹出 window 时出现空指针异常

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

我试图在单击 listView 中的按钮后弹出 window,然后通过在框外单击将其关闭。但是,当我尝试单击按钮时出现此错误。

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

我不能完全理解它抛出空指针异常是我做错了什么。也许我编码不正确,但即使在我回去查看它之后,它对我来说似乎也是正确的。任何帮助将不胜感激。

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;

    }
}

不要在 information 按钮视图中搜索 R.id.orders,而是在 convertView 中搜索

旧代码:

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

新代码:

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