PDA

View Full Version : لیست کردن تعدادی آیتم در RecyclerView



kamran_14
چهارشنبه 03 مهر 1398, 10:05 صبح
سلاممن برای لیست کردن تعدادی آیتم در RecyclerView این کدها رو نوشتم ولی با این خطا مواجه میشوم.ایراد کدهای من چی میتونه باشه


D/AndroidRuntime: Shutting down VME/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shop, PID: 3607
android.view.InflateException: Binary XML file line #24: 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 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(Layo utInflater.java:761)
at android.view.LayoutInflater.createViewFromTag(Layo utInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflate r.java:858)
at android.view.LayoutInflater.rInflateChildren(Layou tInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflate r.java:861)
at android.view.LayoutInflater.rInflateChildren(Layou tInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater .java:518)
at android.view.LayoutInflater.inflate(LayoutInflater .java:426)
at com.example.shop.Adapter.ProductAdapter.onCreateVi ewHolder(ProductAdapter.java:35)
at com.example.shop.Adapter.ProductAdapter.onCreateVi ewHolder(ProductAdapter.java:19)
at androidx.recyclerview.widget.RecyclerView$Adapter. createViewHolder(RecyclerView.java:6794)
at androidx.recyclerview.widget.RecyclerView$Recycler .tryGetViewHolderForPositionByDeadline(RecyclerVie w.java:5975)
at androidx.recyclerview.widget.RecyclerView$Recycler .getViewForPosition(RecyclerView.java:5858)
at androidx.recyclerview.widget.RecyclerView$Recycler .getViewForPosition(RecyclerView.java:5854)
at androidx.recyclerview.widget.LinearLayoutManager$L ayoutState.next(LinearLayoutManager.java:2230)
at androidx.recyclerview.widget.LinearLayoutManager.l ayoutChunk(LinearLayoutManager.java:1557)
at androidx.recyclerview.widget.LinearLayoutManager.f ill(LinearLayoutManager.java:1517)
at androidx.recyclerview.widget.LinearLayoutManager.o nLayoutChildren(LinearLayoutManager.java:612)
at androidx.recyclerview.widget.RecyclerView.dispatch LayoutStep2(RecyclerView.java:3924)
at androidx.recyclerview.widget.RecyclerView.dispatch Layout(RecyclerView.java:3641)
at androidx.recyclerview.widget.RecyclerView.onLayout (RecyclerView.java:4194)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.RelativeLayout.onLayout(RelativeLay out.java:1079)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLay out.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.ja va:261)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.LinearLayout.setChildFrame(LinearLa yout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearL ayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout. java:1494)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLay out.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.ja va:261)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.LinearLayout.setChildFrame(LinearLa yout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearL ayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout. java:1494)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLay out.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.ja va:261)
at com.android.internal.policy.DecorView.onLayout(Dec orView.java:726)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.view.ViewRootImpl.performLayout(ViewRootIm pl.java:2346)
at android.view.ViewRootImpl.performTraversals(ViewRo otImpl.java:2068)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl .java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(Vi ewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Chor eographer.java:874)
at android.view.Choreographer.doCallbacks(Choreograph er.java:686)
at android.view.Choreographer.doFrame(Choreographer.j ava:621)
at android.view.Choreographer$FrameDisplayEventReceiv er.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751 )
at android.os.Handler.dispatchMessage(Handler.java:95 )
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.jav a:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:776)
Application terminated.




من در productActivity اینارو نوشتم:

package com.example.shop;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.example.shop.Adapter.ProductAdapter;
import com.example.shop.Models.Products;


import java.util.ArrayList;
import java.util.List;


public class ProductActivity extends AppCompatActivity {
List<Products> productsModelsList=new ArrayList<Products> ( );
RecyclerView recyclerView;
ProductAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_product );




recyclerView=findViewById ( R.id.rlv_product );
adapter=new ProductAdapter ( productsModelsList,this );


recyclerView.setLayoutManager ( new LinearLayoutManager ( this, RecyclerView.VERTICAL, false ) );
recyclerView.setAdapter ( adapter );
setData ();




}


private void setData()
{
productsModelsList.add ( new Products ( ResourcesCompat.getDrawable ( getResources (),R.drawable.a2,null ),"asdasda","kyukyu6768" ) );
productsModelsList.add ( new Products ( ResourcesCompat.getDrawable ( getResources (),R.drawable.a3,null ),"asdasda","kyukyu6768" ) );
//Log.i ( "myapp", "setData: "+ productsModelsList.size ());
adapter.notifyDataSetChanged ();
}
}




و در productModel این کدها رو:

package com.example.shop.Models;

import android.graphics.drawable.Drawable;


public class Products {
private Drawable img_product;
private String nameProduct;
private String priceProduct;


public Products(Drawable img_product, String nameProduct, String priceProduct) {
this.img_product = img_product;
this.nameProduct = nameProduct;
this.priceProduct = priceProduct;
}


public Drawable getImg_product() {
return img_product;
}


public void setImg_product(Drawable img_product) {
this.img_product = img_product;
}


public String getNameProduct() {
return nameProduct;
}


public void setNameProduct(String nameProduct) {
this.nameProduct = nameProduct;
}


public String getPriceProduct() {
return priceProduct;
}


public void setPriceProduct(String priceProduct) {
this.priceProduct = priceProduct;
}
}




و در productAdapter این کدها رو:

package com.example.shop.Adapter;import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.shop.Models.Products;
import com.example.shop.R;
import java.util.ArrayList;
import java.util.List;


public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder>{
private List<Products> products=new ArrayList<Products> ( );
private Context context;


public ProductAdapter(List<Products> products, Context context) {
this.products = products;
this.context = context;
}




@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from ( parent.getContext () ).inflate ( R.layout.products_list_row,parent,false);//inflater.inflate (R.layout.products_list_row,parent);
return new ProductViewHolder(view);
}


@Override
public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
Products oP=products.get ( position );
holder.txt_priceProduct.setText ( oP.getPriceProduct () );
holder.txt_nameProduct.setText (oP.getNameProduct () );
holder.img_imgProduct.setImageDrawable (oP.getImg_product () );
}


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


public class ProductViewHolder extends RecyclerView.ViewHolder {
public ImageView img_imgProduct;
public TextView txt_nameProduct;
public TextView txt_priceProduct;
public ProductViewHolder(@NonNull View itemView) {
super ( itemView );
img_imgProduct=itemView.findViewById ( R.id.imgProduct );
txt_nameProduct=itemView.findViewById ( R.id.nameProduct );
txt_priceProduct=itemView.findViewById ( R.id.priceProduct );
}


}
}




لطفا کمکم کنید نمیتونم خطا رو پیدا کنم

rubiks.kde
پنج شنبه 04 مهر 1398, 15:54 عصر
خطای شما توی فایل ProductAdapter و توی خطهای 35 است دلیلش هم اینه احتمالا شی تعریف شده توی xml وجود نداره چون خطای دسترسی null دارید