نمایش نتایج 1 تا 2 از 2

نام تاپیک: لیست کردن تعدادی آیتم در RecyclerView

  1. #1

    Question لیست کردن تعدادی آیتم در RecyclerView

    سلاممن برای لیست کردن تعدادی آیتم در RecyclerView این کدها رو نوشتم ولی با این خطا مواجه میشوم.ایراد کدهای من چی میتونه باشه

    D/AndroidRuntime: Shutting down VM
    E/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.ProductViewHol der>{
    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 );
    }


    }
    }




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

  2. #2
    مدیر بخش آواتار rubiks.kde
    تاریخ عضویت
    آبان 1390
    محل زندگی
    مشهد
    پست
    1,537

    نقل قول: لیست کردن تعدادی آیتم در RecyclerView

    خطای شما توی فایل ProductAdapter و توی خطهای 35 است دلیلش هم اینه احتمالا شی تعریف شده توی xml وجود نداره چون خطای دسترسی null دارید
    YES I AM Qt


    Code Less
    Create More
    Deploy Everywhere

تاپیک های مشابه

  1. مشکل فیلتربندی آیتم های RecyclerView
    نوشته شده توسط sobhan90 در بخش Android Studio
    پاسخ: 1
    آخرین پست: چهارشنبه 09 اسفند 1396, 10:29 صبح
  2. پاسخ: 7
    آخرین پست: دوشنبه 15 آبان 1396, 20:14 عصر
  3. پاسخ: 3
    آخرین پست: چهارشنبه 15 دی 1395, 22:46 عصر
  4. انتخاب چند آیتم در RecyclerView
    نوشته شده توسط slr560 در بخش Android Studio
    پاسخ: 0
    آخرین پست: پنج شنبه 05 شهریور 1394, 23:51 عصر
  5. سوال: تعداد ایتم های لیست باکس
    نوشته شده توسط Jaxon_hacker_black در بخش C#‎‎
    پاسخ: 2
    آخرین پست: پنج شنبه 19 دی 1392, 22:16 عصر

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •