我无法在Android Studio中将适配器附加到Recyclerview

时间:2018-08-19 12:04:38

标签: java android android-studio android-recyclerview

我正在尝试将适配器添加到recyclerview,但是以下错误向我显示:

错误:

 android.view.InflateException: Binary XML file line #32: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolea

n java.lang.String.equals(java.lang.Object)' on a null object reference

它指向适配器类中的以下行

vv = LayoutInflater.from(context).inflate(R.layout.custom_offerpro,parent,false);

public class AmazingproAdapter extends RecyclerView.Adapter<AmazingproAdapter.PostViewHolder> {

在适配器..

适配器代码:

public class AmazingproAdapter extends RecyclerView.Adapter<AmazingproAdapter.PostViewHolder> {

    List<ProductAmazing> offerpro;
    Context context;
    View vv;
    ProductAmazing Amazingproduct;
    ImageView img;
    public AmazingproAdapter(List<ProductAmazing> offerpro, Context context) {
        this.offerpro = offerpro;
        this.context = context;
    }

    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        vv = LayoutInflater.from(context).inflate(R.layout.custom_offerpro,parent,false);
        return new PostViewHolder(vv) ;
    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        Amazingproduct = offerpro.get(position);
        holder.txtcaption.setText(Amazingproduct.getCaption());
        holder.txtpreprice.setText(String.valueOf(Amazingproduct.getPreprice()));
        holder.txtnewprice.setText(String.valueOf(Amazingproduct.getNewprice()));
        Picasso.with(context).load(Config.IMAGES_URL + "amazingpro/" + Amazingproduct.getImg()).into(img);
    }

    @Override
    public int getItemCount() {
        return offerpro.size();
    }

    public class PostViewHolder extends RecyclerView.ViewHolder{
        TextView txtcaption,txtpreprice,txtnewprice;
        public PostViewHolder(View itemView) {
            super(itemView);
            txtcaption = itemView.findViewById(R.id.offerpro_caption);
            txtpreprice = itemView.findViewById(R.id.offerpro_preprice);
            txtnewprice = itemView.findViewById(R.id.offerpro_newprice);
            img = itemView.findViewById(R.id.offerpro_img);
        }
    }
}

xml自定义视图:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_offerpro"
    app:cardElevation="0dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <ImageView
        android:id="@+id/offerpro_img"
        android:layout_width="match_parent"
        android:layout_height="165dp" />

    <TextView
        android:id="@+id/offerpro_caption"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:hint="عنوان" />

    <view
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="20dp"
        android:background="@color/black" />

        <TextView
            android:id="@+id/offerpro_preprice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            android:hint="163.000"
            android:textColor="@color/colorPrimary"
            android:textSize="16sp"
            android:textStyle="bold" />

    <TextView
        android:id="@+id/offerpro_newprice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="12dp"
        android:hint="110.000"
        android:textColor="@color/black"
        android:textStyle="bold" />

    </LinearLayout>
</android.support.v7.widget.CardView>

MainActivity.java

private void getamazingdata() {
    StringRequest request = new StringRequest(
            Config.URL_GETOFFERAMAZING,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    list = Config.Jsonamazingparser(response);
                    if(list!=null) {
                        adapter = new AmazingproAdapter(list, MainActivity.this);
                        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                        recyclerView.setAdapter(adapter);
                    }else{
                        Toast.makeText(activity, "List is empty", Toast.LENGTH_SHORT).show();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("Amazing offer response is  : " , " Error " + error.getMessage());
        }
    }
    );
    RequestQueue queue = Volley.newRequestQueue(activity);
    queue.add(request);
}

1 个答案:

答案 0 :(得分:1)

您的XML中有

<view
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="20dp"
        android:background="@color/black" />

请注意,v应该是大写字母 像这样

<View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="20dp"
            android:background="@color/black" />