0
這是我的單身類:凌空請求隊列返回null
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;
}
這是從主要活動的方法來獲得職位:
public void getJsonPosts(){
String url = "http://linagrey.blogspot.com/feeds/posts/default?alt=json";
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET,url,null, new Response.Listener<JSONObject>() {
@Override
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);
}
這是錯誤輸出,我不知道關於如何解釋這些錯誤信息:
05-31 08:44:14.880 2653-2653/wait.com.linasblog E/AndroidRuntime: FATAL EXCEPTION: main
Process: wait.com.linasblog, PID: 2653
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:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
at wait.com.linasblog.RequestSingleton.getRequestQueue(RequestSingleton.java:51)
at wait.com.linasblog.RequestSingleton.<init>(RequestSingleton.java:24)
at wait.com.linasblog.RequestSingleton.getInstance(RequestSingleton.java:44)
at wait.com.linasblog.MainActivity.getJsonPosts(MainActivity.java:204)
at wait.com.linasblog.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我發佈了類似的問題,但沒有得到答案。
仍不能解決problem..can't實例化的單例類... –