简体   繁体   中英

Can`t Pass a Model Class to my BaseAdapter

Error Shown:

Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference.

The error surface at the gridView.setAdapter(adapter); but am able to print response from API only to pass it to the adapter for layout to handle the display.

Am suggesting converting the Model Class into a list of array of which is not actually going my way.
Have tried modelName.toArray() and many others.

Thank you as i await your response .

My Model Class

    public class MensWear {

        private int id, user_id;
        private String first_wear;

        public MensWear(int id,int user_id,String first_wear) {
            this.id = id;
           this.user_id = user_id;
            this.first_wear = first_wear;
        }

        public int getId() {
            return id;
        }

        public int getUser_id() {
            return user_id;
        }

        public String getFirst_wear() {
           return first_wear;
        }
    }

My Base Adapter Class

    public class PhoneAdapter extends BaseAdapter {

        private Context context;
        private List<MensWear>  mensWears;
        public String imageUrlFromServer = "http:/10.0.2.2:5757/api/public/images/";

        public PhoneAdapter(Context context, List<MensWear> mensWears) {
            this.context = context;
            this.mensWears = mensWears;
        }

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

        @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) {
            final MensWear mensWear = mensWears.get(i);

            if (view == null) {
                final LayoutInflater layoutInflater = LayoutInflater.from(context);
                view = layoutInflater.inflate(R.layout.phone_list, null);
            }
            //For text
            TextView prdId = view.findViewById(R.id.mensWearIdForHompePage);
            prdId.setText(prdId.toString());

            //For images
            final ImageView imageView = view.findViewById(R.id.phoneImages);
            if(!TextUtils.isEmpty(mensWear.getFirst_wear())){

     Picasso.with(context).load(imageUrlFromServer+mensWear.getFirst_wear())
    .into(imageView);
            }
            return view;
        }

    }

My Class extending Fragment

    public class Phones extends Fragment {

        private GridView gridView;
        private List<MensWear> mensWears;
        private PhoneAdapter adapter;

        public Phones() {
            // Required empty public constructor
        }

        @Override
        public View onCreateView(LayoutInflater inflater,ViewGroup container,
                             Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_phones,container,false);
        }

        @Override
        public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState) {
            super.onViewCreated(view,savedInstanceState);

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

            gridView = view.findViewById(R.id.gridHolder);
            Call<MensWearResponse> wearResponseCall = 
    Service.getInstance().getApi().getAllMensWears();
            wearResponseCall.enqueue(new Callback<MensWearResponse>() {
            @Override
            public void onResponse(Call<MensWearResponse> call,Response<MensWearResponse> response) {
                mensWears = response.body().getMensWears();
                for (MensWear mwr : mensWears){
                    Log.d("Name", mwr.getFirst_wear());
                }
                adapter = new PhoneAdapter(getActivity(), mensWears);                
                gridView.setAdapter(adapter);

            }

            @Override
            public void onFailure(Call<MensWearResponse> call,Throwable t) {

            }
        });
      }
    }

My Layout Files The Grid Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context="com.nelbuc.nelbuc.goodWears"
    >

    <Grid
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:columnWidth="100dp"
        android:numColumns="2"
        android:verticalSpacing="24dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="spacingWidthUniform"
        android:id="@+id/gridHolder"
        >

    </Grid>

</RelativeLayout>

The List Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/phoneList"
        android:layout_width="190dp"
        android:background="#fff"
        android:layout_height="180dp"
        >

        <TextView
            android:id="@+id/mensWearIdForHompePage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="Apple"
            android:textStyle="normal|italic"
            android:textSize="25dp" />

        <ImageView
            android:id="@+id/phoneImages"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            />
    </RelativeLayout>
</RelativeLayout>

Error message clearly says that GridView is null. There is no GridView in your layout, there is only Grid . Change Grid to GridView .

In your layout, You are using only Grid . Change from Grid to GridView

Create ArrayList and add the model ie MensWear into that list like this following and then pass this list into your adpater :

ArrayList<MensWear > arrayList_mens = new ArrayList<MensWear>();

for (MensWear mwr : mensWears)
{
   Log.d("Name", mwr.getFirst_wear());

   arrayList_mens.add(mwr); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM