Java 回收视图崩溃
一般来说,我对Android和编程都是新手。我正在尝试加载带有一些数据的RecyclerView。 在调试模式下,直到应用程序崩溃时,才会显示错误。 这里使用的主要数据是一个名为Lettura的实体,它是房间数据库的一部分 在这里,我调用并使用一些数据填充视图:Java 回收视图崩溃,java,android,android-recyclerview,Java,Android,Android Recyclerview,一般来说,我对Android和编程都是新手。我正在尝试加载带有一些数据的RecyclerView。 在调试模式下,直到应用程序崩溃时,才会显示错误。 这里使用的主要数据是一个名为Lettura的实体,它是房间数据库的一部分 在这里,我调用并使用一些数据填充视图: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vedi_letture_simple);
ArrayList<Lettura> letture = new ArrayList<>();
Date date = new Date(1989, 1, 1);
Lettura a = new Lettura("a", "b", "c", 14.5, date, 1);
letture.add(a);
RecyclerView recView = (RecyclerView) findViewById(R.id.lettura_recyclerview);
LetturaAdapter mlAdapter = new LetturaAdapter(this, letture); //made final
recView.setAdapter(mlAdapter);
// use a linear layout manager
LinearLayoutManager lll = new LinearLayoutManager(this);
recView.setLayoutManager(lll);
下面是RecyclerView的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorScreenBackground">
<android.support.v7.widget.RecyclerView
android:id="@+id/lettura_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
最后,这里是元素的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:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/barcodeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="barcode"
app:layout_constraintEnd_toEndOf="@+id/dateTextView"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/dateTextView" />
<TextView
android:id="@+id/descriptionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="8dp"
android:text="description"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/barcodeTextView"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/priceTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="price"
app:layout_constraintEnd_toStartOf="@+id/dateTextView"
app:layout_constraintTop_toTopOf="@+id/dateTextView" />
<TextView
android:id="@+id/dateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:text="date"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/quantityTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="8dp"
android:text="quantity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/priceTextView"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/barcodeTextView"
app:layout_constraintTop_toTopOf="@+id/dateTextView"
app:layout_constraintVertical_bias="0.0" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_weight="1"
android:background="#008AD4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
</android.support.constraint.ConstraintLayout>
</LinearLayout>
我想错误就在这里:
TextView quanT = hholded.quantityItemView;
quanT.setText(current.getQuantity());
current.getQuantity返回整数而不是字符串。也许,你可以试试:
TextView quanT = hholded.quantityItemView;
quanT.setText(current.getQuantity().toString());
尝试执行此操作-在onBindViewHolder中替换-
quanT.setText(current.getQuantity());
quanT.setText(current.getQuantity());
与
setText方法只接受字符串值,数量为整数值。因此,此处可能会崩溃。Replace
与
setText仅适用于字符串
请同时分享logcat…您介意发布logcat的Stacktrace吗?请发布您的错误Logs@W0rmH0le您建议添加。toString?它起作用了!toString将其转换为字符串,并+使其成为字符串..两者都可以工作same@gionamantovani很高兴我能帮忙。。。。我已经删除了我的答案,因为我认为它不会有帮助。。。我很高兴你的代码现在可以工作了
quanT.setText(current.getQuantity());
quanT.setText(current.getQuantity()+"");
quanT.setText(current.getQuantity());
quanT.setText(String.valueOf(current.getQuantity()));