英文:
Android Java Error after trying to connect to Firestore
问题
Sure, here's the translation of the provided content:
我想在Android Studio中创建一个RecyclerView,并在List Fragment中用我的Firestore数据填充它。
以下是我用于此操作的教程:
错误信息:
2020-10-21 20:01:05.074 5691-5691/de.abc.storagekeeper E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.abc.storagekeeper, PID: 5691
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.abc.storagekeeper/de.abc.storagekeeper.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
...
MainActivity:
public class MainActivity extends AppCompatActivity {
private FirestoreRecyclerAdapter<ProductModel, ProductViewHolder> adapter;
private FirebaseAnalytics mFirebaseAnalytics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView recyclerView = findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Firebase analytics initializaton
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
//Firebase Firestore Connection to RecycleView
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("Products")
.orderBy("id", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<ProductModel> options = new FirestoreRecyclerOptions.Builder<ProductModel>()
.setQuery(query, ProductModel.class)
.build();
adapter = new FirestoreRecyclerAdapter<ProductModel, ProductViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull ProductModel model) {
holder.setProductName(model.getMname());
}
@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_list, parent, false);
return new ProductViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
private class ProductViewHolder extends RecyclerView.ViewHolder {
private View view;
ProductViewHolder(View itemView) {
super(itemView);
view = itemView;
}
void setProductName(String productName) {
TextView textView = view.findViewById(R.id.text_View);
textView.setText(productName);
}
}
}
ProductModel:
public class ProductModel {
private String mid;
private String msellprice;
private String mpurchasedprice;
private String mname;
private String mdescription;
private String mdeliverydate;
private String mquality;
public ProductModel(){}
public ProductModel(String id, String sellprice, String purchasedprice, String name, String description, String deliverydate, String quality){
mid = id;
msellprice = sellprice;
mpurchasedprice = purchasedprice;
mname = name;
mdescription = description;
mdeliverydate = deliverydate;
mquality = quality;
}
// Getters and setters for each field...
}
activity_main.xml:
<!-- XML content for activity_main -->
fragment_list.xml(用于显示数据的部分):
<!-- XML content for fragment_list -->
我是初学者,希望您能帮助我解决这个问题。
英文:
I want to create a RecyclerView in Android studio and fill it with my Firestore data in a List Fragment.
Here is the Tutorial I used for this:
The error message:
2020-10-21 20:01:05.074 5691-5691/de.abc.storagekeeper E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.abc.storagekeeper, PID: 5691
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.abc.storagekeeper/de.abc.storagekeeper.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
at de.abc.storagekeeper.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
MainActivity:
public class MainActivity extends AppCompatActivity {
private FirestoreRecyclerAdapter<ProductModel, ProductViewHolder> adapter;
private FirebaseAnalytics mFirebaseAnalytics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView recyclerView = findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Firebase analytics initializaton
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
//Firebase Firestore Connection to RecycleView
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("Products")
.orderBy("id", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<ProductModel> options = new FirestoreRecyclerOptions.Builder<ProductModel>()
.setQuery(query, ProductModel.class)
.build();
adapter = new FirestoreRecyclerAdapter<ProductModel, ProductViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull ProductModel model) {
ProductModel productModel = new ProductModel();
holder.setProductName(productModel.getMname());
}
@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_list, parent, false);
return new ProductViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
private class ProductViewHolder extends RecyclerView.ViewHolder {
private View view;
ProductViewHolder(View itemView) {
super(itemView);
view = itemView;
}
void setProductName(String productName) {
TextView textView = view.findViewById(R.id.text_View);
textView.setText(productName);
}
}
}
ProductModel:
public class ProductModel {
private String mid;
private String msellprice;
private String mpurchasedprice;
private String mname;
private String mdescription;
private String mdeliverydate;
private String mquality;
public ProductModel(){}
public ProductModel(String id, String sellprice, String purchasedprice, String name, String description, String deliverydate, String quality){
mid = id;
msellprice = sellprice;
mpurchasedprice = purchasedprice;
mname = name;
mdescription = description;
mdeliverydate = deliverydate;
mquality = quality;
}
public String getMid() {
return mid;
}
public void setMid(String name){
mname = name;
}
public String getMsellprice() {
return mid;
}
public void setMsellprice(String name){
mname = name;
}
public String getMpurchasedprice() {
return mid;
}
public void setMpurchasedprice(String name){
mname = name;
}
public String getMname() {
return mid;
}
public void setMname(String name){
mname = name;
}
public String getMdescription() {
return mid;
}
public void setMdescription(String name){
mname = name;
}
public String getMdeliverydate() {
return mid;
}
public void setMdeliverydate(String name){
mname = name;
}
public String getMquality() {
return mid;
}
public void setMquality(String name){
mname = name;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.StorageKeeper.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/Theme.StorageKeeper.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
fragment_list.xml(where i want to show the data):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".ListFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
I'm a beginner and hope you can help me with this.
答案1
得分: 0
你得到了一个 NullPointerException
,是因为你在一个 null
对象上调用了 .setLayoutManager()
方法。这是因为当视图尚不存在时,findViewById()
会返回 null
。将这个赋值操作(以及依赖它的内容)移到 onViewCreated()
重写方法中:
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
RecyclerView recyclerView = findViewById(R.id.list);
// 现在 recyclerView 不为 null
}
英文:
You are getting a NullPointerException
, because you are calling .setLayoutManager()
on a null
object. This is because findViewById()
returns null
when the View doesn't exist yet. Move this assignment (and the stuff that depends on it) into the onViewCreated()
override method:
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
RecyclerView recyclerView = findViewById(R.id.list);
// recyclerView is not null now
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论