user8816412
user8816412

Reputation:

Recyclerview causes app to crash

Android Dev Newbie here. I am using recyclerview in my MainActivity and the app keeps crashing.

Any help would be appreaciated!

EDIT : Sorry, I am new here. I have attached the Logcat. And the the other xml files. Thanks

This is my code:

list_layout.xml :

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="140dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView_Desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/imageView"
                android:layout_centerHorizontal="true"
                android:text="Description"
                android:textSize="25sp" />

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_marginTop="0dp"
                app:srcCompat="@drawable/ic_launcher_background" />

            <TextView
                android:id="@+id/textView_Name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/imageView"
                android:layout_centerHorizontal="true"
                android:text="Title"
                android:textAppearance="@style/TextAppearance.AppCompat"
                android:textColor="@color/Black"
                android:textSize="36sp" />
        </RelativeLayout>

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

activity_main.xml :

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="e.wolverine2.recyclerview.MainActivity">


    <android.support.v7.app.AlertController.RecycleListView

        android:id="@+id/recyclerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:dividerHeight="5dp">

    </android.support.v7.app.AlertController.RecycleListView>


</RelativeLayout>



public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
ProductAdapter productAdapter;
List<Product> productList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    System.out.println("TAG : 1");

    productList = new ArrayList<>();

    productList.add(new Product(
       1,"Laptop","New Computer",R.drawable.ic_launcher_background
    ));

    productList.add(new Product(
            2, "Mobile Phone", "New Phone", R.drawable.ic_launcher_foreground
    ));


    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);

    productAdapter = new ProductAdapter(this, productList);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(productAdapter);

}

}

ProductAdapter.java Class:

public class ProductAdapter extends 

RecyclerView.Adapter<ProductAdapter.ProductViewHolder>{

private Context context;
private List<Product> productList;


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

@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.list_layout, null);


    ProductViewHolder productViewHolder = new ProductViewHolder(view);

    return new ProductViewHolder(view);
}

@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {

    Product product = productList.get(position);
    holder.textView_Title.setText(product.getTitle());
    holder.textView_Desc.setText(product.getDesc());

    holder.imageView.setImageDrawable(context.getResources().getDrawable(product.getImage()));
}

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

class ProductViewHolder extends RecyclerView.ViewHolder {


    ImageView imageView;
    TextView textView_Title;
    TextView textView_Desc;


    public ProductViewHolder(View itemView) {
        super(itemView);

        imageView = itemView.findViewById(R.id.imageView);
        textView_Title = itemView.findViewById(R.id.textView_Name);
        textView_Desc = itemView.findViewById(R.id.textView_Desc);
    }
}

}

Logcat :

FATAL EXCEPTION: main Process: e.wolverine2.recyclerview, PID: 3738 java.lang.RuntimeException: Unable to start activity ComponentInfo{e.wolverine2.recyclerview/e.wolverine2.recyclerview.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.v7.app.AlertController.RecycleListView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3003) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6816) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451) Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.v7.app.AlertController.RecycleListView Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.v7.app.AlertController.RecycleListView Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.app.AlertController.RecycleListView" on path: DexPathList[[zip file "/data/app/e.wolverine2.recyclerview-1/base.apk", zip file "/data/app/e.wolverine2.recyclerview-1/split_lib_dependencies_apk.apk", zip file "/data/app/e.wolverine2.recyclerview-1/split_lib_slice_0_apk.apk", zip file "/data/app/e.wolverine2.recyclerview-1/split_lib_slice_1_apk.apk", zip file "/data/app/e.wolverine2.recyclerview-1/split_lib_slice_2_apk.apk", zip file "/data/app/e.wolverine2.recyclerview-1/split_lib_slice_3_apk.apk", zip file "/data/app/e.wolverine2.recyclerview-1/split_lib_slice_4_apk.apk", zip file "/data/app/e.wolverine2.recyclerview-1/split_lib_slice_5_apk.apk", zip file "/data/app/e.wolverine2.recyclerview-1/split_lib_slice_6_apk.apk", zip file "/data/app/e.wolverine2.recyclerview-1/split_lib_slice_7_apk.apk", zip file "/data/app/e.wolverine2.recyclerview-1/split_lib_slice_8_apk.apk", zip file "/data/app/e.wolverine2.recyclerview-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/e.wolverine2.recyclerview-1/lib/arm, /system/lib, /vendor/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:380) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) at android.view.LayoutInflater.createView(LayoutInflater.java:616) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734) at android.view.LayoutInflater.rInflate(LayoutInflater.java:865) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828) at android.view.LayoutInflater.inflate(LayoutInflater.java:525) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at android.view.LayoutInflater.inflate(LayoutInflater.java:378) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) at e.wolverine2.recyclerview.MainActivity.onCreate(MainActivity.java:21) at android.app.Activity.performCreate(Activity.java:6977) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154)

Upvotes: 0

Views: 497

Answers (1)

Amrish Kakadiya
Amrish Kakadiya

Reputation: 1023

change this

<android.support.v7.app.AlertController.RecycleListView
        android:id="@+id/recyclerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:dividerHeight="5dp">
    </android.support.v7.app.AlertController.RecycleListView>

to

<android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
          />

AlertController.RecycleListView and RecyclerView both are different.

Upvotes: 0

Related Questions