gpt4 book ai didi

java - 在 Android Studio 中实现 Recyclerview 时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 01:19:50 25 4
gpt4 key购买 nike

我正在尝试创建一个回收 View 来显示 Android Studio Version3.5 中的图书列表。

当我尝试单击必须在回收 View 中显示图书列表的按钮时,我的应用程序崩溃了。

这是我的源代码:

1.AllBooksJava Activity

package com.example.mybooks;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;


public class AllBooksActivity extends AppCompatActivity {

private RecyclerView bookRecView;

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


bookRecView = findViewById(R.id.my_recycler_view);

BooksRecViewAdapter adapter = new BooksRecViewAdapter(this);
bookRecView.setAdapter(adapter);
bookRecView.setLayoutManager(new LinearLayoutManager(this));

ArrayList<Book> books = new ArrayList<>();
books.add(new Book("muna madan", "Laxmi Parsad Devkota", 300, "", "muna madan novel"));
books.add(new Book("Palpasa Cafe", "Narayan Wagle", 300, "", "palpasa cafe"));
books.add(new Book("Summer Love", "Subin Bhattrai", 390, "", "summer love"));

//setting books to adapter
adapter.setBooks(books);


}
}

2.图书回收 View 适配器

package com.example.mybooks;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class BooksRecViewAdapter extends RecyclerView.Adapter<BooksRecViewAdapter.ViewHolder> {
private static final String TAG = "BooksRecViewAdapter";

//initialize book class
private ArrayList<Book> books = new ArrayList<>();
private Context context;

//contr for context


public BooksRecViewAdapter(Context context) {
this.context = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//ceating a view
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_book_rec_view, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: called");
holder.bookName.setText(books.get(position).getName());
//click event
holder.bookName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, books.get(position).getName() + "selected ", Toast.LENGTH_SHORT).show();
}
});

}

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

//inner class
public class ViewHolder extends RecyclerView.ViewHolder {
//implement const to avoid error
private ImageView bookImage;
private TextView bookName;

public ViewHolder(@NonNull View itemView) {
super(itemView);
bookImage = itemView.findViewById(R.id.bookImage);
bookName = itemView.findViewById(R.id.bookName);
}
}

public void setBooks(ArrayList<Book> books) {
this.books = books;
notifyDataSetChanged();
}
}

3.读书课

private String name;
private String author;
private int pages;
private String imageUrl;
private String description;


public Book(String name,String author,int pages,String imageUrl,String description){
this.name=name;
this.author=author;
this.pages=pages;
this.imageUrl=imageUrl;
this.description=description;
}

public String getName(){
return name;
}

public void setName(String name){
this.name=name;
}

public String getAuthor(){
return author;
}

public void setAuthor(String author){
this.author=author;
}

public int getPages(){
return pages;
}

public void setPages(int pages){
this.pages=pages;
}

public String getImageUrl(){
return imageUrl;
}

public void setImageUrl(String imageUrl){
this.imageUrl=imageUrl;
}

public String getDescription(){
return description;
}

public void setDescription(String description){
this.description=description;
}

@Override
public String toString(){
return"Book{"+
"name='"+name+'\''+
", author='"+author+'\''+
", pages="+pages+
", imageUrl='"+imageUrl+'\''+
", description='"+description+'\''+
'}';
}

4.书籍布局

<?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=".AllBooksActivity"
android:padding="20dp"
>

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

</RelativeLayout>

5.book自定义布局

<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
>

<ImageView
android:layout_width="140dp"
android:layout_height="140dp"
android:id="@+id/bookImage"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bookName"
android:text="Book Name"
android:layout_below="@+id/bookImage"
android:layout_centerHorizontal="true"
android:textSize="14sp"
android:layout_marginTop="7dp"/>
</RelativeLayout>

Logcat 错误

2019-09-12 22:47:28.710 18954-18986/com.example.mybooks E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
2019-09-12 22:47:28.710 18954-18986/com.example.mybooks E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
2019-09-12 22:47:43.666 18954-18954/com.example.mybooks E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mybooks, PID: 18954
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mybooks/com.example.mybooks.AllBooksActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class android.support.v7.widget.RecyclerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class android.support.v7.widget.RecyclerView
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.support.v7.widget.RecyclerView
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.RecyclerView" on path: DexPathList[[zip file "/data/app/com.example.mybooks-QSMXr1k2Y13cVZg37-7bvw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.mybooks-QSMXr1k2Y13cVZg37-7bvw==/lib/x86, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:606)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.mybooks.AllBooksActivity.onCreate(AllBooksActivity.java:25)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

请帮帮我,我不知道哪里出了问题。我的应用程序被停止。

最佳答案

问题是您正在使用 androidx RecyclerView 并在 xml 中从支持库实现 Recyclerview

使用这个:

androidx.recyclerview:recyclerview:"your code version"

而不是这个:

android.support.v7.widget.RecyclerView

关于java - 在 Android Studio 中实现 Recyclerview 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57911294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com