来自singleton类的requestQueue返回null值

时间:2016-05-31 12:41:40

标签: java android json android-volley

凌空图书馆不会从博客中提取帖子。我是初学者,我被困住了,我真的需要完成这件事。我不知道错误来自哪里。

这是MainActivity.java:

public void onResponse(JSONObject response) {
        try {
            JSONObject feedResponse =  response.getJSONObject("feed");
            JSONArray entryArrayPosts = feedResponse.getJSONArray("entry");
            BlogPost bp;
            for (int i = 0; i < entryArrayPosts.length(); i++){
                bp = new BlogPost();
                //Get the entry as an array and loop through
                JSONObject entryBlogPost = entryArrayPosts.getJSONObject(i);

                //Get the title from the array
                JSONObject postObject = entryBlogPost.getJSONObject("title");
                //JSONObject postHtmlContent = entryBlogPost.getJSONObject(KEYS.POST_JSON_OBJECT_HTML_CONTENT);

                //GET LINK ARRAY FROM JSON
                JSONArray linkAddress = entryBlogPost.getJSONArray(KEYS.POST_JSON_ARRAY_OBJECT);

                if(entryBlogPost.has(KEYS.MEDIA_THUMBNAIL)) {
                    JSONObject imageObjectUrl = entryBlogPost.getJSONObject(KEYS.MEDIA_THUMBNAIL);
                    String thumbnailUrl = imageObjectUrl.getString(KEYS.MEDIA_THUMBNAIL_URL);
                    bp.setmUrlThumbnail(thumbnailUrl);
                }

                //for loop for link array object
                for (int j = 0;j < linkAddress.length(); j++){
                    JSONObject eachLink = linkAddress.getJSONObject(4);
                    String htmlUrl = eachLink.getString(KEYS.POST_HTML_LINK);
                    bp.setmLinkContent(htmlUrl);
                }


                //Get title string contained in Title Object
                String title = postObject.getString(KEYS.POST_TITLE);
                Log.v(TAG, "OBJECTS HAVE THIS PROPERTIES: " + i + " " + title);


                //Set Properties Gotten
                bp.setmTitle(title);

                Log.v(TAG, "OBJECTS HAVE THIS PROPERTIES: " + i + " " + bp.toString());
                //Add a blog post Object to the Arraylist
                postArrayList.add(bp);


            }
           // Log.v(TAG, "Arraylist SIZE " + postArrayList.size());

            for(BlogPost posts : postArrayList){
                Log.v(TAG, posts + " ");
            }

            // Toast.makeText(this.,response.toString(),Toast.LENGTH_LONG).show();

        }catch(JSONException e){
            Log.v(TAG, e.toString());
        }
    }
},new Response.ErrorListener(){
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.v(TAG, error.toString());
    }
});
RequestSingleton.getInstance(this).addToRequestQueue(jsonRequest);
}

这是我的单身人士课程:

public class RequestSingleton {

    private static RequestSingleton mInstance ;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static Context mCtx;

        private RequestSingleton(Context context){
            mCtx = context;
            mRequestQueue = getRequestQueue();

            mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache(){

                private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20);

                @Override
                public Bitmap getBitmap(String url) {
                    return cache.get(url);
                }

                @Override
                public void putBitmap(String url, Bitmap bitmap) {
                    cache.put(url, bitmap);
                }
            });
        }

    public static synchronized RequestSingleton getInstance(Context context){
        if (mInstance == null){
            mInstance = new RequestSingleton(context);
        }
        return mInstance;
    }

    private RequestQueue getRequestQueue(){
        if(mRequestQueue == null){
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req){
        getRequestQueue().add(req);
    }
    public ImageLoader getImageLoader(){
        return mImageLoader;
    }
}

它打印出一个错误“尝试在空对象引用上调用虚方法上下文”

java.lang.RuntimeException: Unable to start activity ComponentInfo{wait.com.linasblog/wait.com.linasblog.MainActivity}:
   java.lang.NullPointerException: Attempt to invoke virtual method
   'android.content.Context android.content.Context.getApplicationContext()'
   on a null object reference
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java...

0 个答案:

没有答案