Android Java错误,在尝试连接到Firestore后发生。

huangapple go评论70阅读模式
英文:

Android Java Error after trying to connect to Firestore

问题

Sure, here's the translation of the provided content:

我想在Android Studio中创建一个RecyclerView,并在List Fragment中用我的Firestore数据填充它。
以下是我用于此操作的教程:

> https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android

错误信息:

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:

> https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android

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 &#39;void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)&#39; 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 &#39;void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)&#39; 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&lt;ProductModel, ProductViewHolder&gt; 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(&quot;Products&quot;)
.orderBy(&quot;id&quot;, Query.Direction.ASCENDING);
FirestoreRecyclerOptions&lt;ProductModel&gt; options = new FirestoreRecyclerOptions.Builder&lt;ProductModel&gt;()
.setQuery(query, ProductModel.class)
.build();
adapter = new FirestoreRecyclerAdapter&lt;ProductModel, ProductViewHolder&gt;(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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;com.google.android.material.appbar.AppBarLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:theme=&quot;@style/Theme.StorageKeeper.AppBarOverlay&quot;&gt;
&lt;androidx.appcompat.widget.Toolbar
android:id=&quot;@+id/toolbar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;?attr/actionBarSize&quot;
android:background=&quot;?attr/colorPrimary&quot;
app:popupTheme=&quot;@style/Theme.StorageKeeper.PopupOverlay&quot; /&gt;
&lt;/com.google.android.material.appbar.AppBarLayout&gt;
&lt;include layout=&quot;@layout/content_main&quot; /&gt;
&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

fragment_list.xml(where i want to show the data):

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.ListFragment&quot;&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;TextView
android:id=&quot;@+id/text_View&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;TextView&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/list&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/FrameLayout&gt;

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
}

huangapple
  • 本文由 发表于 2020年10月22日 02:16:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64469450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定