java.lang.IllegalArgumentException:视图不能为空;在 Android 中实现 UIL displayImage 函数时

java.lang.IllegalArgumentException: view must not be null; when implementing UIL displayImage function in Android

我正在使用 Universal Image Loader 库加载图像,但出现此异常。我不明白这个异常的原因。谁能帮忙?提前致谢。

 E/j v .lang.Illegal ArgumentException: view must not be null
 E/j v .l ng.Illeg l rgumentException: java .lang.Illegal ArgumentException: view must not be null
 E/j v .l ng.Illeg l rgumentException:      t com.nostra13.universalimageloader.core.im ge w re.View w re.<init>(View w re.j v :70)
 E/j v .l ng.Illeg l rgumentException:      t com.nostr 13.univers lim gelo der.core.im ge w re.View w re.<init>(View w re.j v :50)
 E/j v .l ng.Illeg l rgumentException:      t com.nostr 13.univers lim gelo der.core.im ge w re.Im geView w re.<init>(Im geView w re.j v :44)
 E/j v .l ng.Illeg l rgumentException:      t com.nostr 13.univers lim gelo der.core.Im geLo der.displ yIm ge(Im geLo der.j v :303)
 E/j v .l ng.Illeg l rgumentException:      t project1.ri fy.com.im gecentercrop.M in ctivity.im gePopupWindow(M in ctivity.j v :219)
 E/j v .l ng.Illeg l rgumentException:      t project1.ri fy.com.im gecentercrop.M in ctivity.onClick(M in ctivity.j v :143)
 E/j v .l ng.Illeg l rgumentException:      t  ndroid.view.View.performClick(View.j v :4232)
 E/j v .l ng.Illeg l rgumentException:      t  ndroid.view.View$PerformClick.run(View.j v :17298)
 E/j v .l ng.Illeg l rgumentException:      t  ndroid.os.H ndler.h ndleC llb ck(H ndler.j v :615)
 E/j v .l ng.Illeg l rgumentException:      t  ndroid.os.H ndler.disp tchMess ge(H ndler.j v :92)
 E/j v .l ng.Illeg l rgumentException:      t  ndroid.os.Looper.loop(Looper.j v :137)
 E/j v .l ng.Illeg l rgumentException:      t  ndroid. pp. ctivityThre d.m in( ctivityThre d.j v :4921)
 E/j v .l ng.Illeg l rgumentException:      t j v .l ng.reflect.Method.invokeN tive(N tive Method)
 E/j v .l ng.Illeg l rgumentException:      t j v .l ng.reflect.Method.invoke(Method.j v :511)
 E/j v .l ng.Illeg l rgumentException:      t com. ndroid.intern l.os.ZygoteInit$Method nd rgsC ller.run(ZygoteInit.j v :1027)
 E/j v .l ng.Illeg l rgumentException:      t com. ndroid.intern l.os.ZygoteInit.m in(ZygoteInit.j v :794)
 E/j v .l ng.Illeg l rgumentException:      t d lvik.system.N tiveSt rt.m in(N tive Method)
 E/Sp nn bleStringBuilder: SP N_EXCLUSIVE_EXCLUSIVE sp ns cannot h ve   zero length
 E/Sp nn bleStringBuilder: SP N_EXCLUSIVE_EXCLUSIVE sp ns cannot h ve   zero length

我试图在弹出窗口中显示图像 window。这是我的代码:

public void imagePopupWindow(ArrayList<String> imageList) {
    try {
        final PopupWindow pwindotwo;

        LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.popupmenu,
                (ViewGroup) findViewById(R.id.popup_element));
        pwindotwo = new PopupWindow(layout, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);

        ImageLoader imageLoader = ImageLoader.getInstance();
        DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
                .cacheOnDisc(true).resetViewBeforeLoading(true).build();

        ImageView imageView= (ImageView) findViewById(R.id.imgView2);;

        String array[] = new String[imageList.size()];

        try {
            for (int j = 0; j <imageList.size(); j++) {
                array[j] = imageList.get(j);
            }
        }
        catch (Exception e) {
            Log.e(e.getClass().getName(), e.getMessage(),e);
        }

        for(int j=0;j<imageList.size();j++) {

           try {
               imageLoader.displayImage(array[j], imageView, options);
               Log.e("gg", "arraylist" + array);
           }
           catch (Exception e){
               Log.e(e.getClass().getName(), e.getMessage(),e);
           }
       }

        pwindotwo .setOutsideTouchable(true);
        pwindotwo .setBackgroundDrawable(new ShapeDrawable());
        pwindotwo .setTouchInterceptor(new View.OnTouchListener() { // or whatever you want
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
                {
                    pwindotwo .dismiss();
                    return true;
                }
                return false;
            }
        });

        pwindotwo.showAtLocation(layout, Gravity.CENTER_VERTICAL, 0, 0);
        pwindotwo.showAsDropDown(layout);
        pwindotwo.setFocusable(true);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

根据堆栈跟踪

 ImageView imageView= (ImageView) findViewById(R.id.imgView2);;

为空。它可能是你膨胀的 layout 的一部分。将其更改为

ImageView imageView= (ImageView) layout.findViewById(R.id.imgView2);;