无法将模型类传递给我的BaseAdapter

时间:2018-12-01 16:32:22

标签: java android android-fragments

显示的错误:

  

尝试在空对象引用上调用虚拟方法'void android.widget.GridView.setAdapter(android.widget.ListAdapter)'。

位于gridView.setAdapter(adapter);的错误表面,但只能从API打印响应,仅将其传递给适配器以进行布局以处理显示。

我建议将模型类转换为数组列表,这实际上并非我所愿。
尝试过modelName.toArray()等。

感谢您等待我的回复

我的模特班

    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;
        }
    }

我的基本适配器类

    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;
        }

    }

我的班级延伸片段

    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) {

            }
        });
      }
    }

我的布局文件 网格布局

<?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>

列表布局

<?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>

2 个答案:

答案 0 :(得分:0)

错误消息明确指出GridView为空。您的布局中没有GridView,只有Grid。将Grid更改为GridView

答案 1 :(得分:0)

在布局中,您仅使用Grid。从Grid更改为GridView

创建ArrayList并将模型(例如MensWear)添加到该列表中,如下所示,然后将此列表传递到您的adpater中:

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

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

   arrayList_mens.add(mwr); 
}