我的代码中没有错误,我的应用程序在我的设备上运行就像它应该但屏幕上没有显示任何内容。这是一个奇怪的问题,因为Null错误只是在我测试运行应用程序之前的一个位置,现在它变得更糟而没有更改代码......
这个应用程序是一个非常简单的概念,我试图这样做。我只想为JSON Feed提取图像并将其放入RecyclerView中。就这样。如果你能提出一种更有效的方法,那就太棒了。这让我花了很长时间才完成,而且我即将退出。
以下是我通过代码收到的错误。
FATAL EXCEPTION: main
Process: zafir.com.motive, PID: 4868
java.lang.RuntimeException: Unable to start activity
ComponentInfo{zafir.com.motive/zafir.com.motive.Activites.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.Context
zafir.com.motive.Volley.MyApplication.getApplicationContext()' on a
null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2370)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2432)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5310)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.Context
zafir.com.motive.Volley.MyApplication.getApplicationContext()' on a
null object reference
at zafir.com.motive.Volley.MyApplication.getAppContext(MyApplication.java:26)
at zafir.com.motive.Volley.Singleton.<init(Singleton.java:19)
at zafir.com.motive.Volley.Singleton.getsInstance(Singleton.java:44)
at zafir.com.motive.Activites.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:6865)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2323)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2432)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5310)
at java.lang.reflect.Method.invoke(Native Method)
MainActivity
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import zafir.com.motive.R;
import zafir.com.motive.RecyclerView.Photo;
import zafir.com.motive.RecyclerView.RecyclerAdapter;
import zafir.com.motive.Volley.Singleton;
public class MainActivity extends AppCompatActivity
{
private static final String API_URL = "LINK";
private ArrayList<Photo> listPhotos = new ArrayList<>();
private ImageLoader imageLoader;
private RequestQueue requestQueue;
private RecyclerAdapter recyclerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Sending JSON Volley request
Singleton singleton = Singleton.getsInstance();
requestQueue = singleton.getmRequestQueue();
//Initializing the RecyclerView and it's layout
RecyclerView recView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerAdapter = new RecyclerAdapter(this);
recView.setAdapter(recyclerAdapter);
StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.HORIZONTAL);
recView.setLayoutManager(gridLayoutManager);
sendJsonRequest();
}
private void sendJsonRequest()
{
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, API_URL, (String) null, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
listPhotos = parseJSONResponse(response);
recyclerAdapter.setPhotos(listPhotos);
}
}, new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
}
});
requestQueue.add(request);
}
private ArrayList<Photo> parseJSONResponse(JSONObject response)
{
if (response == null || response.length() == 0)
{
try
{
assert response != null;
JSONObject objectRes = response.getJSONObject("response");
JSONArray arrayPosts = objectRes.getJSONArray("posts");
for (int i = 0; i < arrayPosts.length(); i++)
{
JSONObject object = arrayPosts.getJSONObject(i);
JSONArray arrayPhotos = object.getJSONArray("photos");
for (int j = 0; j < arrayPhotos.length(); j++)
{
JSONObject firstPhoto = arrayPhotos.getJSONObject(i);
JSONObject originalSize = firstPhoto.optJSONObject("original_size");
String url = originalSize.getString("url");
Photo photo = new Photo();
photo.setImage(url);
listPhotos.add(photo);
}
}
}catch(JSONException e)
{
}
}
return listPhotos;
}
}
RecyclerAdapter
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import java.util.ArrayList;
import zafir.com.motive.R;
import zafir.com.motive.Volley.Singleton;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>
{
private ArrayList<Photo> listPhotos = new ArrayList<>();
private Singleton singleton;
private ImageLoader imageLoader;
private LayoutInflater layoutInflater;
public RecyclerAdapter(Context context)
{
singleton = Singleton.getsInstance();
imageLoader = singleton.getImageLoader();
layoutInflater = LayoutInflater.from(context);
}
public void setPhotos(ArrayList<Photo> photos)
{
this.listPhotos = photos;
notifyItemRangeChanged(0, listPhotos.size());
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View recyclerViewLayout = inflater.inflate(R.layout.recyclerview_layout,parent,false);
ViewHolder viewHolder = new ViewHolder(recyclerViewLayout);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position)
{
Photo currentPhoto = listPhotos.get(position);
String urlThumbnail = currentPhoto.getImage();
if(urlThumbnail != null)
{
imageLoader.get(urlThumbnail, new ImageLoader.ImageListener()
{
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate)
{
holder.imageView.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error)
{
}
});
}
}
@Override
public int getItemCount()
{
return listPhotos.size();
}
public class ViewHolder extends RecyclerView.ViewHolder
{
public ImageView imageView;
public ViewHolder(View itemView)
{
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.rec_image);
}
}
}
的Singleton
import android.graphics.Bitmap;
import android.util.LruCache;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class Singleton
{
private static Singleton sInstance = null;
private RequestQueue mRequestQueue;
private ImageLoader imageLoader;
private Singleton()
{
mRequestQueue = Volley.newRequestQueue(MyApplication.getAppContext());
//Managing Image cache.
ImageLoader imageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache()
{
private LruCache<String, Bitmap> cache = new LruCache<>((int) (Runtime.getRuntime().maxMemory()/1024/8));
@Override
public Bitmap getBitmap(String url)
{
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap)
{
cache.put(url,bitmap);
}
});
}
public static Singleton getsInstance()
{
if(sInstance == null)
{
sInstance = new Singleton();
}
return sInstance;
}
public RequestQueue getmRequestQueue()
{
return mRequestQueue;
}
public ImageLoader getImageLoader()
{
return imageLoader;
}
所有MyApplication
import android.app.Application;
import android.content.Context;
public class MyApplication extends Application
{
private static MyApplication sInstance;
@Override
public void onCreate()
{
super.onCreate();
sInstance = this;
}
public static MyApplication getsInstance()
{
return sInstance;
}
public static Context getAppContext()
{
return sInstance.getApplicationContext();
}
}
照片(模特类)
public class Photo
{
private String Image;
//----------Get-and-Set----------
public String getImage()
{
return Image;
}
public void setImage(String image)
{
this.Image = image;
}
//----------Constructor----------
public Photo(String urlImage)
{
this.Image = urlImage;
}
public Photo()
{
}
//----------ToString-------------
@Override
public String toString()
{
return "urlImage" + Image;
}
}
Activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activates.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>