0
votes

I have been trying to get bunch of images like gallery inside my fragment of the navigation bar item. here the code goes like this.

public class ImageAdapter extends BaseAdapter { private Context context;

public Integer[] images = {
        R.drawable.base1, R.drawable.base2,
        R.drawable.base3, R.drawable.base2,
        R.drawable.base1, R.drawable.base3,

};

public ImageAdapter(Context c) {
    context = c;
}
@Override
public int getCount() {
    return images.length;
}

@Override
public Object getItem(int position) {
    return images(position);
}

private Object images(int position) {

    return images(position);
}


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

@Override
public View getView(int position, View ConvertView, ViewGroup parent) {
    ImageView imageview = new ImageView(context);
    imageview.setImageResource(images[position]);
    imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageview.setLayoutParams(new GridView.LayoutParams(240, 240));
    return imageview;
}

And in the main activity I have set this

GridView gridView = (GridView) findViewById(R.id.gridView);

    gridView.setAdapter(new ImageAdapter(this));`

but whenever i run my app i get this error. Here is the logcat

07-20 08:34:49.974 4608-4608/com.example.android.navigation D/AndroidRuntime: Shutting down VM 07-20 08:34:49.975 4608-4608/com.example.android.navigation E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.navigation, PID: 4608 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.navigation/com.example.android.navigation.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' 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 android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.example.android.navigation.MainActivity.onCreate(MainActivity.java:32) 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) 

Note: Don't get confused with the images R.drawable.base123. I just wrote 3 to make it short. I have set 20 images on my workspace.

2
According to stacktrace, your gridview is null. Have you define it?dniHze
If i define gridview in main xml then the images will appear in every other navigation item too. I want different images to be appeared on different item clicked on navigation bar.Arjun Thakuri

2 Answers

0
votes

Try to declare "gridView" before the onCreate method

private GridView mGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
    setContentView(R.layout.yourLayout);
  mGridView = (GridView) findViewById(R.id.gridView);

 mGridView.setAdapter(new ImageAdapter(this));

}

0
votes

Check onCreate method of your activity, may be you are referring to the wrong R.layout file

public void onCreate(Bundle savedInstanceState)
{ 
        super.onCreate(savedInstanceState);
        // Check this
        setContentView(R.layout.filters); 
}