我无法将适配器连接到安卓工作室中的回收器视图



我正在尝试将适配器添加到回收器视图,但以下错误显示我:

错误:

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>

主要活动.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);
}

在你的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" />

最新更新