Null pointer exception in volley singleton
I've copied the volleySingleton code from developer.android.com and tried to get an image from a URL in a recycler view, but at the time of loading data app crashes. I've tried some fixes but they haven't worked
Here is the volleySingleton class copied from developer.android.com
public class MyApplication extends Application {
private static MyApplication mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private MyApplication(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 MyApplication getInstance(Context context) {
if (mInstance == null) {
mInstance = new MyApplication(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
Here is the logcat report
java.lang.NullPointerException
at com.aninesoft.meltwishbeta.MyApplication.getRequestQueue(MyApplication.java:61)
at com.aninesoft.meltwishbeta.MyApplication.(MyApplication.java:31)
at com.aninesoft.meltwishbeta.MyApplication.getInstance(MyApplication.java:52)
at com.aninesoft.meltwishbeta.RecyclerViewDataAdapter.onBindViewHolder(RecyclerViewDataAdapter.java:45)
at com.aninesoft.meltwishbeta.RecyclerViewDataAdapter.onBindViewHolder(RecyclerViewDataAdapter.java:19)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5277)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5310)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4568)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1371)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1034)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:744)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1180)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:757)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14832)
at android.view.ViewGroup.layout(ViewGroup.java:4640)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1996)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1753)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$
Here is the adapter class
public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ViewHolder> {
private Context context;
public ImageLoader mImageLoader;
//List of superHeroes
List<ListOfData> superHeroes;
public RecyclerViewDataAdapter(List<ListOfData> superHeroes, Context context){
super();
//Getting all the superheroes
this.superHeroes = superHeroes;
this.context=context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_row, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListOfData superHero = superHeroes.get(position);
mImageLoader = MyApplication.getInstance(context).getImageLoader();
mImageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView1, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
holder.imageView1.setImageUrl(superHero.getImageUrl(), mImageLoader);
holder.textViewName1.setText(superHero.getName());
//holder.textViewRank.setText(String.valueOf(superHero.getRank()));
//holder.textViewRealName.setText(superHero.getRealName());
//holder.textViewCreatedBy.setText(superHero.getCreatedBy());
//holder.textViewFirstAppearance.setText(superHero.getFirstAppearance());
String powers = "";
/* for(int i = 0; i<superHero.getPowers().size(); i++){
powers+= superHero.getPowers().get(i);
}*/
//holder.textViewPowers.setText(powers);
}
@Override
public int getItemCount() {
return superHeroes.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public NetworkImageView imageView1;
public NetworkImageView imageView2;
public TextView textViewName1;
public TextView textViewName2;
public TextView textViewRank;
public TextView textViewRealName;
public TextView textViewCreatedBy;
public TextView textViewFirstAppearance;
public TextView textViewPowers;
public ViewHolder(View itemView) {
super(itemView);
imageView1 = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
//imageView2 = (NetworkImageView) itemView.findViewById(R.id.thumbnail2);
textViewName1 = (TextView) itemView.findViewById(R.id.textViewName);//textViewName2 = (TextView) itemView.findViewById(R.id.category_title_two);
/* textViewRank= (TextView) itemView.findViewById(R.id.textViewRank);
textViewRealName= (TextView) itemView.findViewById(R.id.textViewRealName);
textViewCreatedBy= (TextView) itemView.findViewById(R.id.textViewCreatedBy);
textViewFirstAppearance= (TextView) itemView.findViewById(R.id.textViewFirstAppearance);
textViewPowers= (TextView) itemView.findViewById(R.id.textViewPowers);
*/
}
}
}
and Json part
public class Pants extends Fragment {
//Creating a List of superheroes
public Context context;
public List<ListOfData> listSuperHeroes;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
public Pants() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.tshirts_pants_shirts, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
//Calling method to get data
getData();
return view;
}
private void getData() {
//Showing a progress dialog
final ProgressDialog loading = ProgressDialog.show(getActivity(), "Loading Data", "Please wait...", false, false);
//Creating a json array request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ProductConfig.DATA_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Dismissing progress dialog
loading.dismiss();
//calling method to parse json array
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}
//This method will parse json data
private void parseData(JSONArray array) {
listSuperHeroes = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
ListOfData superHero = new ListOfData();
JSONObject json = null;
try {
json = array.getJSONObject(i);
superHero.setImageUrl(json.getString(ProductConfig.TAG_IMAGE_URL));
superHero.setName(json.getString(ProductConfig.TAG_NAME));
/*superHero.setRank(json.getInt(ProductConfig.TAG_RANK));
superHero.setRealName(json.getString(ProductConfig.TAG_REAL_NAME));
superHero.setCreatedBy(json.getString(ProductConfig.TAG_CREATED_BY));
superHero.setFirstAppearance(json.getString(ProductConfig.TAG_FIRST_APPEARANCE));
*/
ArrayList<String> powers = new ArrayList<String>();
JSONArray jsonArray = json.getJSONArray(ProductConfig.TAG_POWERS);
for (int j = 0; j < jsonArray.length(); j++) {
powers.add(((String) jsonArray.get(j)) + "n");
}
superHero.setPowers(powers);
} catch (JSONException e) {
e.printStackTrace();
}
listSuperHeroes.add(superHero);
}
adapter = new RecyclerViewDataAdapter(listSuperHeroes, context);
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
//Initializing our superheroes list
}
}
链接地址: http://www.djcxy.com/p/93158.html
上一篇: 添加视图具有RecyclerView作为父视图,但视图不是真正的孩子
下一篇: 凌空单身中的空指针异常