java.lang.IllegalArgumentException:view不能为null;在Android中实现UIL displayImage函数时

时间:2015-10-15 12:41:39

标签: android android-layout android-imageview universal-image-loader android-popupwindow

我使用通用图片加载器库加载图片,我得到了这个例外。我无法理解这个例外的原因。有人可以帮忙吗?提前谢谢。

 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$2.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

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

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

1 个答案:

答案 0 :(得分:2)

相应的堆栈跟踪

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

为空。它可能是你膨胀的layout的一部分。用

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