错误:
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 {
在适配器中。。
public class AmazingproAdapter extends RecyclerView.Adapter {
List offerpro;
Context context;
View vv;
ProductAmazing Amazingproduct;
ImageView img;
public AmazingproAdapter(List 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自定义视图:
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">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:id="@+id/offerpro_img"
android:layout_width="match_parent"
android:layout_height="165dp" />
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="عÙÙاÙ" />
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" />
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" />
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" />
main活动.java
private void getamazingdata() {
StringRequest request = new StringRequest(
Config.URL_GETOFFERAMAZING,
new Response.Listener() {
@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);
}