当前位置: 首页 > 知识库问答 >
问题:

为什么在android中使用回收器视图时出现运行时异常

冀弘济
2023-03-14

我正在学习回收器视图,只是构建了一个基本列表来显示名称和编号,但却遇到了运行时异常

这是密码

mainactivity.java

package com.example.listviewandrecyclerview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

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

public class MainActivity extends AppCompatActivity {
    private List< Person> personData;
    private RecyclerView recyclerView;
    private RecyclerViewAdapter recyclerViewAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        personData = new ArrayList();
        personData.add( new Person("Hemant","1"));
        personData.add( new Person("Prashant","144"));
        personData.add( new Person("sagar","331"));
        personData.add( new Person("mayank","31"));
        personData.add( new Person("mayur","13"));
        personData.add( new Person("Hemant","1"));
        personData.add( new Person("Prashant","144"));
        personData.add( new Person("sagar","331"));
        personData.add( new Person("mayank","31"));
        personData.add( new Person("mayur","13"));

        recyclerViewAdapter = new RecyclerViewAdapter(personData, this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(recyclerViewAdapter);

    }
}

回收器ViewAdpater.java

package com.example.listviewandrecyclerview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.Holder> {

    private List<Person> personList;
    private Context context;

    public RecyclerViewAdapter(List<Person> list, Context context) {
        this.personList = list;
        this.context = context;
    }


    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_layout, parent, false);
        return new Holder(view);
    }

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

        Person currPerson = personList.get(position);

        holder.textViewName.setText(currPerson.getName());
        holder.textViewNumber.setText(currPerson.getNumber());

    }

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


    class Holder extends RecyclerView.ViewHolder {

        public TextView textViewName;
        public TextView textViewNumber;

        public Holder(@NonNull View itemView) {
            super(itemView);
            textViewName = (TextView) itemView.findViewById(R.id.name);
            textViewNumber = (TextView) itemView.findViewById(R.id.number);
        }
    }

}

person.java

package com.example.listviewandrecyclerview;

public class Person  {
    public String Name, Number;

    public Person(String name, String number) {
        this.Name = name;
        this.Number = number;
    }

    public String getName() {
        return Name;
    }

    public String getNumber() {
        return Number;
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="729dp"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

recycler_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp" >

    <androidx.constraintlayout.widget.ConstraintLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="36sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="36sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/name" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

但我得到了这样的excpeton:e/AndroidRuntime:致命异常:主进程:com.example.ListViewandRecolyerView,PID:8041 Android.view.InflateException:二进制XML文件行#2:二进制XML文件行#2:膨胀类AndroidX.CardView.Widget.CardView错误,原因是:Android.View.InflateException:膨胀类AndroidX.CardView.Widget.CardView错误,原因是:java.lang.ClassNotFoundException:在路径:DexPathList[[zip file“Ater.CreateViewFromTag(layoutInflater.java:790)在Android.View.LayoutInflater.java:730)在Android.View.LayoutInflater.Inflate(layoutInflater.java:492)在Android.View.LayoutInflater.Inflate(layoutInflater.423)在Com.Example.ListViewandRecyclerView.RecyclerViewAdapter.OnCreateViewWholder(

共有1个答案

湛宜春
2023-03-14

我试用了您的代码,得到了屏幕截图中提到的解决方案

使用以下解决方案:

>

  • 请尝试使缓存无效并重新启动

    此外,检查是否添加了appcompat、约束布局和物料依赖项

    实现“androidx.appcompat:appcompat:1.2.0”

    实现“AndroidX.ConstraintLayout:ConstraintLayout:2.0.1”

    实现“com.google.android.material:material:1.2.1”

    • 确保已更新SDK

  •  类似资料:
    • 我在里面使用了。我还将设置为false for 支持较低的API

    • 问题内容: 最近,我接受了公司的采访,他们给了我一个编码问题。我得到了与纸牌有关的程序,其中一种方法是将纸牌洗牌。因此,我将该程序编写为: 在上面的代码中,我引发了我最怀疑的 IllegalArgumentException 。在什么情况下实际上应该抛出运行时异常?我们是否应该实际抛出运行时异常? 谢谢 问题答案: 我们是否应该实际抛出运行时异常? 是的,我们应该。运行时异常有特定的用途-它们发出

    • 我最终得到了一个500的错误,这取决于stacktrace,这可能是一个数据格式错误,但我所有的变量都是字符串,所以我根本看不到错误在哪里。此外,我的连接jdbc很好,因为在调试模式下,我可以看到数据库的值,但一旦它进入JSP中,它就不再工作了。你能帮帮我吗? servlet JSP 豆类 BDD连接 堆栈跟踪 我的servlet:

    • 我使用GridLayoutManager实现了一个RecyclerView。 根据数据集中的项目数,spanCount在1-4之间。项目宽度根据跨距计数而变化。如果spancount为4或以上,spancount保留为4。 如果我有5个项目,那么剩下1个项目(1行4个项目,剩下1个项目),位于屏幕左侧,单独一行。我希望它能居中。 我已经尝试设置剩余的项目的边距的编程,并包装这两个回收器视图和个别项

    • 问题内容: 该示例非常简单:我想通过仅显示文本(canvas.drawText())来让用户知道应用程序在做什么。然后,出现我的第一条消息,而不是其他消息。我的意思是,我有一个“ setText”方法,但它不会更新。 该视图的文本绘制仅通过在onDraw();中执行一个drawText来起作用,因此setText更改了文本但不显示它。 有人建议我用SurfaceView替换视图,但是仅进行几次更新