简体   繁体   English

Android Volley:结合JsonObjectRequest和ImageLoader,NullPointerException

[英]Android Volley: Combine JsonObjectRequest & ImageLoader, NullPointerException

I encounter a situation that first the image's url should be get from weibo's API, and then, I wanna show the image based on the URL: 我遇到一种情况,首先应该从微博的API获取图像的url,然后再基于URL显示图像:

The Volley Lib is used here, and Here is my code: 这里使用了Volley Lib,这是我的代码:

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.i("Result", response.toString());
                    try {
                        String weiboNickName = response.getString("name");
                        weiboNameMain.setText(weiboNickName);

                        weiboUrl = response.getString("avatar_hd");

                        weiboUrl = weiboUrl.replace("\\", "");

                        Toast.makeText(MainPage.this, "url: " + weiboUrl, Toast.LENGTH_SHORT).show();                                                       imageLoader = new ImageLoader(queue, new BitmapLruCache(BitmapLruCache.getDefaultLruCacheSize()));

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
            queue.add(jsObjRequest);
            imageLoader.get(weiboUrl,ImageLoader.getImageListener(weiboAvatarMain,R.drawable.user, R.drawable.user));

However, I will get a NullPointerException when use a weiboUrl for the last sentense: 但是,当使用weiboUrl作为最后一个sendense时,我将得到NullPointerException:

     imageLoader.get(weiboUrl,ImageLoader.getImageListener(weiboAvatarMain,R.drawable.user, R.drawable.user));

I know this is because that Volley will create another Thread, so I should not use the reference in the main thread before Thread in Request ended. 我知道这是因为Volley将创建另一个线程,因此在请求中的线程结束之前,我不应该在主线程中使用引用。

Is there some ideas that could use to overcome this problem? 有一些想法可以用来克服这个问题吗?

Thanks in advance. 提前致谢。

Best 最好

---Updated - -更新

The Problem is that I put 问题是我把

imageLoader.get(weiboUrl,ImageLoader.getImageListener(weiboAvatarMain,R.drawable.user, R.drawable.user));

in a wrong position. 在错误的位置。 If I put it within the jsObject's onResponse() method, the problem will be gone. 如果将其放在jsObject的onResponse()方法中,则问题将消失。 Then the image cache could be used. 然后可以使用图像缓存。 ( ImageRequest does not own cache mechanism itself) ImageRequest本身不拥有缓存机制)

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.i("Result", response.toString());
                    try {
                        String weiboNickName = response.getString("name");
                        weiboNameMain.setText(weiboNickName);

                        weiboUrl = response.getString("avatar_hd");

                        weiboUrl = weiboUrl.replace("\\", "");


                        Response.Listener<Bitmap> listener = new Response.Listener<Bitmap>() {
                            @Override
                            public void onResponse(Bitmap bitmap) {
                                String weiboAvatar = ImageHandler.storeImage(bitmap, getRoot(), getWeiboAvatarName());

                                Log.i("store info", weiboAvatar);
                                Editor edit = preference.edit();
                                edit.putString("weiboAvatar", weiboAvatar);
                                edit.apply(); 

                                weiboAvatarMain.setImageBitmap(bitmap);
                            }
                        };

                        ImageRequest imgRequest = new ImageRequest(weiboUrl, listener, 0, 0, null, null);
                        queue.add(imgRequest);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
            queue.add(jsObjRequest);

I use the ImageRequest instead of ImageLoader to overcome this, it works. 我使用ImageRequest而不是ImageLoader来克服这个问题,它可以工作。 The scenario here is the image will not change overtime, so I then save it into the local file. 这里的情况是图像不会随着时间变化,因此我将其保存到本地文件中。

However, as ImageLoader support cache for image reading, I really need the solutions for cache reading. 但是,由于ImageLoader支持用于读取图像的缓存,因此我确实需要用于读取缓存的解决方案。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM