【问题标题】:java.lang.IllegalArgumentException: view must not be null; when implementing UIL displayImage function in Androidjava.lang.IllegalArgumentException:视图不能为空;在Android中实现UIL displayImage函数时
【发布时间】:2016-01-13 22:20:04
【问题描述】:

我正在使用 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$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();
    }
}

【问题讨论】:

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


    【解决方案1】:

    根据堆栈跟踪

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

    为空。它可能是您夸大的layout 的一部分。改变它

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

    【讨论】:

    • 你的意思是 ImageView imageView= (ImageView)R.layout.popupmenu.findViewById(R.id.imgView2);这个??
    • 我的布局文件名为“popupmenu”
    • 你把 ImageView 放在了哪个布局中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多