Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error on setAdapter when developing Gallery view [duplicate]

Basically, I am trying to create an application which has a gallery view of images. I did the gallery view using grid view. Fragment is used, not an activity. But the app crashes when open the gallery session. Here is the code:

XML FILE(fragment_gallery.xml):

<FrameLayout 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"
    android:padding="5dp"
    tools:context="com.example.tonymathew.eliteenglishclub.gallery">

    <!-- TODO: Update blank fragment layout -->

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gridView"
        android:columnWidth="100dp"
        android:stretchMode="columnWidth"

        android:numColumns="auto_fit"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="5dp"
        android:layout_gravity="center" />

</FrameLayout>

Fragment File(gallery.java):

public class gallery extends Fragment {
    GridView gridView;

    public gallery() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_gallery, container, false);

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

        gridView.setAdapter(new ImageAdapter(this));

        return view;
    }

}

Adapter Class(Image Adapter.java):

public class ImageAdapter extends BaseAdapter{
    private Context context;

    public Integer[] images = {
            R.drawable.about, R.drawable.gallery,
            R.drawable.home, R.drawable.invitation,
            R.drawable.register, R.drawable.membership,
            R.drawable.about, R.drawable.gallery,
            R.drawable.home, R.drawable.invitation,
            R.drawable.register, R.drawable.membership,
            R.drawable.about, R.drawable.gallery,
            R.drawable.home, R.drawable.invitation,
            R.drawable.register, R.drawable.membership,
            R.drawable.about, R.drawable.gallery
    };

    public ImageAdapter(Context c){
        context = c;
    }

    public ImageAdapter(gallery g){

    }

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

    @Override
    public Object getItem(int i) {
        return images[i];
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(images[i]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setLayoutParams(new GridView.LayoutParams(240,240));
        return imageView;
    }
}

This is the error that I get:

09-12 22:25:45.359 29157-29157/com.example.tonymathew.eliteenglishclub E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tonymathew.eliteenglishclub, PID: 29157
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
 at com.example.tonymathew.eliteenglishclub.gallery.onCreateView(gallery.java:28)
 at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
 at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
 at android.os.Handler.handleCallback(Handler.java:746)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5443)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
like image 445
Tony Mathew Avatar asked Sep 12 '16 16:09

Tony Mathew


2 Answers

Change

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

with

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

And use getActivity() in place of this when using a fragment

Hope this will help you

like image 94
Neelay Srivastava Avatar answered Nov 18 '22 02:11

Neelay Srivastava


In the Fragment File you should change from

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

To

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

like image 45
Emanuele Gandolfi Avatar answered Nov 18 '22 04:11

Emanuele Gandolfi