我不知道该做什么以及问题来自哪里。我已经尝试了各种方案来获得解决方案,但似乎没有任何结果。 这是错误输出:
06-09 14:33:44.082 3383-3383/wait.com.linasblog E/AndroidRuntime: FATAL EXCEPTION: main
Process: wait.com.linasblog, PID: 3383
java.lang.RuntimeException: Unable to start activity ComponentInfo{wait.com.linasblog/wait.com.linasblog.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void wait.com.linasblog.AppController.addToRequestQueue(com.android.volley.Request)' 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 'void wait.com.linasblog.AppController.addToRequestQueue(com.android.volley.Request)' on a null object reference
at wait.com.linasblog.MainActivity.getJsonPosts(MainActivity.java:208)
at wait.com.linasblog.MainActivity.onCreate(MainActivity.java:42)
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)
这是我的请求方法:
public void getJsonPosts(){
RequestSingleton.getInstance(this.getApplicationContext()).getRequestQueue();
// RequestQueue queue = RequestSingleton.getInstance(this.getApplicationContext()).getRequestQueue();
// queue.start();
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);
}
这是我的Singleton类:
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;
}
public 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;
}
}
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wait.com.linasblog">
<uses-permission android:name = "android.permission.INTERNET"/>
<uses-permission android:name= "android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:name = ".RequestSingleton"
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
在您的RequestSingleton中,您将获得
上的空值public <T> void addToRequestQueue(Request<T> req){
getRequestQueue().add(req);
}
正如您将在错误日志中看到的那样,在日志中显示了一半。