英文:
App keeps stopping whenever I open RecyclerView activity
问题
你的应用在尝试打开带有 RecyclerView 的活动时突然停止工作。你已经尝试了多种方法来在活动中添加 RecyclerView,但都没有成功。其他活动正常运行。
Firestore 正在向你的应用发送数据,每次运行带有 RecyclerView 的活动时数据读取计数都会增加。
你的问题可能是与 Firestore 数据模型和适配器相关的。以下是可能的问题和建议:
-
数据模型问题:确保你的
TalesDetails
类与 Firestore 中的文档数据字段匹配。你的数据模型只包含一个名为Name
的字段。请确保 Firestore 中的文档也有相同的字段,并且它们的类型匹配。 -
适配器问题:你的适配器代码看起来正确,但确保你的 XML 布局文件
recyclerviewtales
中包含一个具有 ID 为tvName
的 TextView,以便正确绑定数据。 -
依赖项问题:你的 Gradle 依赖项似乎没有问题,但确保你的依赖项与你的项目需求相匹配,最好使用最新的库版本。
-
Firestore 数据库规则:确保你的 Firestore 数据库规则允许你的应用读取数据。你可以在 Firebase 控制台中配置数据库规则。
-
异常问题:根据你提供的日志,可能是由于
java.lang.RuntimeException
引发的异常。这可能是由于数据模型不匹配或其他问题引起的。仔细检查你的数据模型和 Firestore 数据,确保它们匹配。
请仔细检查这些方面,尤其是数据模型和适配器的一致性,以查找并解决问题。如果问题仍然存在,请提供更多信息,以便我能够提供更具体的建议。
英文:
My app keeps stopping unexpectedly whenever I try to open the activity with recycler view. I have tried many other ways to add recycler view in my activity.but none of them work. Other activities work flawlessly.
Firestore is sending data to my app, Data Reads count increases every time I run activity with recycler view.
What am I doing wrong?
my logcat error
2020-08-13 15:50:12.895 17761-17761/? E/om.example.rus: Unknown bits set in runtime_flags: 0x8000
2020-08-13 15:50:20.355 17761-17761/com.example.rush E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rush, PID: 17761
java.lang.RuntimeException: Found two getters or fields with conflicting case sensitivity for property: name
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.addProperty(CustomClassMapper.java:736)
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.<init>(CustomClassMapper.java:640)
at com.google.firebase.firestore.util.CustomClassMapper.loadOrCreateBeanMapperForClass(CustomClassMapper.java:377)
at com.google.firebase.firestore.util.CustomClassMapper.convertBean(CustomClassMapper.java:540)
at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(CustomClassMapper.java:253)
at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:100)
at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:187)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:116)
at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:165)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:97)
at com.firebase.ui.firestore.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:23)
at com.firebase.ui.firestore.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:12)
at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:36)
at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
at com.firebase.ui.firestore.FirestoreRecyclerAdapter.getItem(FirestoreRecyclerAdapter.java:86)
at com.firebase.ui.firestore.FirestoreRecyclerAdapter.onBindViewHolder(FirestoreRecyclerAdapter.java:158)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:530)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
2020-08-13 15:50:20.356 17761-17761/com.example.rush E/AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:779)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3080)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2590)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:725)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
MainActivity
package com.example.rush;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
public class MainActivity extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference NameRef = db.collection("TalesSR");
private TalesAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpRecyclerView();
}
private void setUpRecyclerView() {
Query query = NameRef.orderBy("Name", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<TalesDetails> options = new FirestoreRecyclerOptions.Builder<TalesDetails>()
.setQuery(query, TalesDetails.class)
.build();
adapter = new TalesAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
Model Class
public class TalesDetails {
public TalesDetails(String name) {
Name = name;
}
public String Name;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public TalesDetails() {}
}
Adapter Class
package com.example.rush;
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 com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
public class TalesAdapter extends FirestoreRecyclerAdapter<TalesDetails, TalesAdapter.TalesHolder> {
public TalesAdapter(@NonNull FirestoreRecyclerOptions<TalesDetails> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull TalesHolder holder, int position, @NonNull TalesDetails talesDetails) {
holder.textViewTitle.setText(talesDetails.getName());
}
@NonNull
@Override
public TalesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerviewtales,
parent, false);
return new TalesHolder(v);
}
class TalesHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
public TalesHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.tvName);
}
}
}
dependencies
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.firebase:firebase-firestore:21.5.0'
testImplementation 'junit:junit:4.12'
implementation 'com.firebaseui:firebase-ui-firestore:6.3.0'
implementation 'com.google.android.material:material:1.2.0'
implementation 'com.google.firebase:firebase-firestore:21.5.0'
implementation 'androidx.multidex:multidex:2.0.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
答案1
得分: 1
你的代码问题在于你在TalesDetails
类中将name
属性设置为public,并且命名不正确。如果你想使用公共字段,那么不需要添加getter或setter,否则,请将其设置为私有,如下所示:
public class TalesDetails {
private String name; //设置为私有,并以小写字母开头
public TalesDetails() {}
public TalesDetails(String name) {
this.name = name; //注意这里的赋值语句
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name; //注意这里的赋值语句
}
}
注意,现在name
属性的首字母小写。此外,要小心在数据库中将名称也设置为小写。
你还可以检查以下三个答案:类名首字母小写,数据库中首字母大写,类名首字母大写,数据库中首字母小写 和 类名首字母大写,数据库中首字母大写。
英文:
The problem in your code lies in the fact that you are having in your TalesDetails
class, the name
property set as public, and with an incorrect name. If you want to use public fields, then there is no need for adding getters or setters, otherwise, make it private as below:
public class TalesDetails {
private String name; //Is set private and starting with a lowercase
public TalesDetails() {}
public TalesDetails(String name) {
name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
name = name;
}
}
See, the name
property now is starting with a lowercase. Besides that, be careful to have the name set with lowercase as well in the database.
You might also check the following three answers, lowercase letter in class, capital in the database, capital letter in class, lowercase in the database and capital letter in class, capital in the database.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论