繁体   English   中英

Recyclerview导致应用程序崩溃

[英]Recyclerview causes app to crash

Android Dev Newbie在这里。 我在MainActivity中使用了recyclerview,该应用不断崩溃。

任何帮助将不胜感激!

编辑:对不起,我是新来的。 我已经附上了Logcat。 以及其他xml文件。 谢谢

这是我的代码:

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类:

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:

致命异常:主进程:e.wolverine2.recyclerview,PID:3738 java.lang.RuntimeException:无法启动活动ComponentInfo {e.wolverine2.recyclerview / e.wolverine2.recyclerview.MainActivity}:android.view.InflateException:二进制XML文件行#0:二进制XML文件行0:错误膨胀了类android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3003)的android.support.v7.app.AlertController.RecycleListView(在android.app.ActivityThread.handleLaunchActivity( android.app.ActivityThread.-wrap14(ActivityThread.java)(位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1659)处于android.os.Handler.dispatchMessage(Handler.java: 102)at android.os.Looper.loop(Looper.java:154)at android.app.ActivityThread.main(ActivityThread.java:6816)at com.android的java.lang.reflect.Method.invoke(Native Method) .internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1563)位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1 451)原因:android.view.InflateException:二进制XML文件行#0:二进制XML文件行#0:错误膨胀了类android.support.v7.app.AlertController.RecycleListView原因:android.view.InflateException:二进制XML文件行#0:膨胀类android.support.v7.app.AlertController.RecycleListView的错误原因:java.lang.ClassNotFoundException:在路径:DexPathList上找不到类“ android.support.v7.app.AlertController.RecycleListView” [[压缩文件“ /data/app/e.wolverine2.recyclerview-1/base.apk”,压缩文件“ /data/app/e.wolverine2.recyclerview-1/split_lib_dependencies_apk.apk”,压缩文件“ / data / app / e.wolverine2.recyclerview-1 / split_lib_slice_0_apk.apk”,zip文件“ /data/app/e.wolverine2.recyclerview-1/split_lib_slice_1_apk.apk”、zip文件“ /data/app/e.wolverine2.recyclerview- 1 / split_lib_slice_2_apk.apk”,zip文件“ /data/app/e.wolverine2.recyclerview-1/split_lib_slice_3_apk.apk”、zip文件“ /data/app/e.wolverine2.recyclerview-1/split_lib_slice_4_apk.apk”文件“ / data / app / e.wolverine2.recyclerview-1 / split_lib_slice_5_apk.apk”,zip文件“ /data/app/e.wolverine2.recyclerview-1/split_lib_slice_6_apk.apk”、zip文件“ /data/app/e.wolverine2.recyclerview-1/ split_lib_slice_7_apk.apk”,zip文件“ /data/app/e.wolverine2.recyclerview-1/split_lib_slice_8_apk.apk”,zip文件“ /data/app/e.wolverine2.recyclerview-1/split_lib_slice_9_apk.apbr=Directory=native [/data/app/e.wolverine2.recyclerview-1/lib/arm、/system/lib、/vendor/lib]]在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)在java.lang.ClassLoader .loadClass(ClassLoader.java:380)在java.lang.ClassLoader.loadClass(ClassLoader.java:312)在android.view.LayoutInflater.createView(LayoutInflater.java:616)在android.view.LayoutInflater.createViewFromTag(LayoutInflater。于android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)处的java:794)于android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)处android.view.LayoutInflater.rInflate(LayoutInflater.java:865)处 在android.view.LayoutInflater.inflate(LayoutInflater.java:525)在android.view.LayoutInflater.inflate(LayoutInflater.java:427)在android.view.LayoutInflater.inflate(LayoutInflater.java:378)在android.support。 v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)在e.wolverine2.recyclerview.MainActivity.onCreate(MainActivity.java:21)在android.app.Activity.performCreate(Activity.java:6977)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)在android.app。 android.app.ActivityThread.-wrap14(ActivityThread.java)(位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1659)位于android.os.Handler.dispatchMessage()上的ActivityThread.handleLaunchActivity(ActivityThread.java:3064) Handler.java:102),位于android.os.Looper.loop(Looper.java:154)

改变这个

<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>

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

AlertController.RecycleListViewRecyclerView都不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM