我一直收到此错误。我到处环顾四周,但无法解决问题,正在创建一个简单的导航应用程序,并试图将用户的导航历史记录保存到Firebase的回收者视图中。错误
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.maps.contour.contour.HistoryListAdapter.getItemCount(HistoryListAdapter.java:39)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3722)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3527)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
at android.view.View.layout(View.java:17666)
at android.view.ViewGroup.layout(ViewGroup.java:5577)
at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
at android.view.View.layout(View.java:17666)
at android.view.ViewGroup.layout(ViewGroup.java:5577)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17666)
at android.view.ViewGroup.layout(ViewGroup.java:5577)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17666)
at android.view.ViewGroup.layout(ViewGroup.java:5577)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17666)
at android.view.ViewGroup.layout(ViewGroup.java:5577)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17666)
at android.view.ViewGroup.layout(ViewGroup.java:5577)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:730)
at android.view.View.layout(View.java:17666)
at android.view.ViewGroup.layout(ViewGroup.java:5577)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2383)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2105)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1291)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6396)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:876)
at android.view.Choreographer.doCallbacks(Choreographer.java:688)
at android.view.Choreographer.doFrame(Choreographer.java:623)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:862)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6228)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
History.java
package com.maps.contour.contour;
import android.app.UiModeManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageButton;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
public class History extends AppCompatActivity {
private static final String TAG = "History";
private RecyclerView mMainList;
private FirebaseFirestore mFirestore;
private FirebaseAuth mAuth;
private List<HistoryModel> historyList;
private HistoryListAdapter historyListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
historyListAdapter = new HistoryListAdapter(historyList);
historyList = new ArrayList<>();
mMainList = findViewById(R.id.main_list);
mMainList.setHasFixedSize(true);
mMainList.setLayoutManager(new LinearLayoutManager(this));
mMainList.setAdapter(historyListAdapter);
mFirestore = FirebaseFirestore.getInstance();
mAuth = FirebaseAuth.getInstance();
ImageButton btn_Back = findViewById(R.id.btnBack);
UiModeManager uiManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
Objects.requireNonNull(uiManager).setNightMode(UiModeManager.MODE_NIGHT_NO);
loadHistory();
try{
mFirestore.collection("Hello").document(mAuth.getCurrentUser().getEmail()).collection("Hi").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if(e!= null){
Log.d(TAG, "Error: " + e.getMessage());
}
for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()){
if (doc.getType() == DocumentChange.Type.ADDED){
HistoryModel his = doc.getDocument().toObject(HistoryModel.class);
historyList.add(his);
historyListAdapter.notifyDataSetChanged();
}
}
}
});
}catch(Exception e){
Log.d(TAG, "Error: " + e.getMessage());
}
btn_Back.setOnClickListener(v -> finish());
}
private void loadHistory(){
}
}
HistoryModel.java
package com.maps.contour.contour;
public class HistoryModel {
String Start, End;
public HistoryModel(String start, String end) {
Start = start;
End = end;
}
public String getStart() {
return Start;
}
public void setStart(String start) {
Start = start;
}
public String getEnd() {
return End;
}
public void setEnd(String end) {
End = end;
}
}
HistoryListAdapter.java
package com.maps.contour.contour;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class HistoryListAdapter extends RecyclerView.Adapter<HistoryListAdapter.ViewHolder>{
public List<HistoryModel> historyList;
public HistoryListAdapter(List<HistoryModel> historyList){
this.historyList = historyList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.startText.setText(historyList.get(position).getStart());
holder.endText.setText(historyList.get(position).getEnd());
}
@Override
public int getItemCount() {
return historyList.size(); // Error occurs here
}
public class ViewHolder extends RecyclerView.ViewHolder {
View mView;
public TextView startText;
public TextView endText;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
startText = mView.findViewById(R.id.txtStart);
endText = mView.findViewById(R.id.txtEnd);
}
}
}
History.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".History">
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="0dp"
android:layout_height="55dp"
android:background="@drawable/background_nav_bottom"
android:elevation="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageButton
android:contentDescription="@string/back_button"
android:id="@+id/btnBack"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="2dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:backgroundTint="@android:color/transparent"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/icon_back_arrow" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="45dp"
android:text="@string/history"
android:textColor="#9E9E9E"
android:textSize="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/btnBack"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/main_list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout2" />
</android.support.constraint.ConstraintLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content">
<TextView
android:id="@+id/txtStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:text="@string/start"
app:layout_constraintBottom_toTopOf="@+id/txtEnd"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txtEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginStart="15dp"
android:layout_marginTop="5dp"
android:text="@string/end"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtStart" />
</android.support.constraint.ConstraintLayout>
提前谢谢!
答案 0 :(得分:3)
在您的const PortfolioItem = (props) => (
<div>
<h1>A Thing I've Done</h1>
<p>This page is for the item with the id of {props.match.params.id}</p>
</div>
);
中,您在实例之前使用onCreate
。
尝试更改这些行:
historyList
收件人:
historyListAdapter = new HistoryListAdapter(historyList);
historyList = new ArrayList<>();