0

I was following AbhiAndroid tutorial, I want my first, MainActivity to start a new, SecondActivity, and my app is basically for displaying a grid of different Android logos and enlarging them to original size when clicked on; However, I get

FATAL EXCEPTION: main
Process: fuckoff.com.gridviewexample, PID: 3127
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference at fuckoff.com.gridviewexample.CustomAdapter.getView(CustomAdapter.java:30)

And yes, I've read all the existing thread, starting from this java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference, this findViewByID returns null

and some other, still didn't find how that could be applied to my case

the class that's the reason of the trouble is:

package fuckoff.com.gridviewexample;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class CustomAdapter extends BaseAdapter{
          Context context;
          int logos[];
          LayoutInflater inflter;
          public CustomAdapter(Context applicationContext, int[] logos){
          this.context = applicationContext;
          this.logos = logos;
          inflter = (LayoutInflater.from(applicationContext));}

    @Override
    public int getCount(){return logos.length;}

    @Override
    public Object getItem(int i){return null;}

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

    @Override
    public View getView(int i,View view,ViewGroup viewGroup){
            view = inflter.inflate(R.layout.activity_gridview,null);
            ImageView icon = (ImageView)view.findViewById(R.id.icon);
            icon.setImageResource(logos[i]);
            return view;}
}
ufulol
  • 13
  • 1
  • 3

1 Answers1

0

You need to add viewGroup parameter in place of null in your inflate method. Replace it like this in your getView method.

    view = inflter.inflate(R.layout.activity_gridview, viewGroup);
Aalap Patel
  • 2,058
  • 2
  • 17
  • 31
  • Now it shows `FATAL EXCEPTION: main Process: fuckoff.com.gridviewexample, PID: 2764 android.view.InflateException: Binary XML file line #98: addView(View, LayoutParams) is not supported in AdapterView Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView at android.widget.AdapterView.addView` – ufulol Oct 05 '17 at 17:19