I want to parse the JSON array to grid view parsing is done but Error raising in gridview adapter


I want to parse the JSON array to grid view parsing is done but Error raising in gridview adapter



Here I attached my whole code to make this clear and to understand the error
Below is my Parsing code using Volley


private void getData()
{
final ProgressDialog loading = ProgressDialog.show(this,"Loading","please wait",false,false);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,DATA_URL,null,new Response.Listener<JSONObject>(){

@Override
public void onResponse(JSONObject response) {
loading.dismiss();
showGrid(response.optJSONArray("data"));
}
},
new Response.ErrorListener(){

@Override
public void onErrorResponse(VolleyError error) {
loading.dismiss();
Toast.makeText(getApplicationContext(),error.getMessage()+",hello everybody",Toast.LENGTH_LONG).show();
}
}
);

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);
}



Below Is Adapter class



I am getting error in adapter class @"Custom volley request" as Null pointer exception. Please help me to solve that issue.


public class GridHomeAdapter extends BaseAdapter {

//Imageloader to load images
private ImageLoader imageLoader;

//Context
private Context context;

//Array List that would contain the urls and the titles for the images
private ArrayList<String> category_image;
private ArrayList<String> category_name;

public GridHomeAdapter(Context context, ArrayList<String> category_image, ArrayList<String> category_name)
{
this.context = context;
this.category_image = category_image;
this.category_name = category_name;
}

@Override
public int getCount() {
return category_image.size();
}

@Override
public Object getItem(int i) {
return category_image.get(i);
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

//Creating a linear or frame layout
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(linearLayout.VERTICAL);

//NetworkImageView
NetworkImageView networkImageView = new NetworkImageView(context);

//Initializing ImageLoader
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(category_image.get(i), ImageLoader.getImageListener(networkImageView,R.mipmap.ic_launcher,android.R.drawable.ic_dialog_alert));

//Setting the image url to load
networkImageView.setImageUrl(category_image.get(i),imageLoader);

//Creating a textview to show the title
TextView textView = new TextView(context);
textView.setText(category_name.get(i));

//Scaling the imageview
networkImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
networkImageView.setLayoutParams(new GridView.LayoutParams(200,200));

//Adding views to the layout
linearLayout.addView(textView);
linearLayout.addView(networkImageView);

//Returnint the layout
return linearLayout;
}



Below is CustomVolleyRequest class code. I saw som tutorial on this and wrote this code.


public class CustomVolleyRequest {

private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;

private CustomVolleyRequest(Context context) {
this.context = context;
this.requestQueue = getRequestQueue();

imageLoader = new ImageLoader(requestQueue, 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 CustomVolleyRequest getInstance(Context context) {
if (customVolleyRequest == null) {
customVolleyRequest = new CustomVolleyRequest(context);
}
return customVolleyRequest;
}
public RequestQueue getRequestQueue()
{
if (requestQueue == null){
Cache cache = new DiskBasedCache(context.getCacheDir(),10*1024*1024);
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache,network);
requestQueue.start();
}
return requestQueue;
}
public ImageLoader getImageLoader(){
return imageLoader;
}



}
THis the logcat





Post the logcat
– sHOLE
Jun 30 at 6:25





attach the stacktrace for the exception
– Udit
Jun 30 at 6:35





posted my logcat
– Mercedes Muthu
Jun 30 at 11:17









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV