Android int java.util.List.size()和#x27上的错误;在空对象引用Firebase上

Android int java.util.List.size()和#x27上的错误;在空对象引用Firebase上,android,android-recyclerview,Android,Android Recyclerview,我一直在犯这个错误。我到处都找过了,但我无法修复它,我正在创建一个简单的导航应用程序,并试图将用户导航历史保存到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.g

我一直在犯这个错误。我到处都找过了,但我无法修复它,我正在创建一个简单的导航应用程序,并试图将用户导航历史保存到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(){





    }
}
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);

        }
    }
}
package com.maps.contour.contour;
导入android.support.annotation.NonNull;
导入android.support.v7.widget.RecyclerView;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.TextView;
导入java.util.List;
公共类HistoryListAdapter扩展了RecyclerView.Adapter{
公开名单历史学者;
公共HistoryListAdapter(列表historyList){
this.historyList=historyList;
}
@非空
@凌驾
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
View=LayoutFlater.from(parent.getContext()).flate(R.layout.list_项,parent,false);
返回新的ViewHolder(视图);
}
@凌驾
public void onBindViewHolder(@NonNull ViewHolder,int位置){
holder.startText.setText(historyList.get(position.getStart());
holder.endText.setText(historyList.get(position.getEnd());
}
@凌驾
public int getItemCount(){
return historyList.size();//此处出错
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
视图视图;
公共文本视图开始文本;
公共文本视图endText;
公共视图持有者(视图项视图){
超级(项目视图);
mView=项目视图;
startText=mView.findviewbyd(R.id.txtStart);
endText=mView.findviewbyd(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>


提前谢谢

在您的
onCreate
中,您正在实例之前使用
historyList

尝试更改以下行:

    historyListAdapter = new HistoryListAdapter(historyList);
    historyList = new ArrayList<>();
historyListAdapter=新的historyListAdapter(historyList);
historyList=新的ArrayList();
致:

historyList=newarraylist();
historyListAdapter=新的historyListAdapter(historyList);

在您的
onCreate
中,您正在实例之前使用
历史列表

尝试更改以下行:

    historyListAdapter = new HistoryListAdapter(historyList);
    historyList = new ArrayList<>();
historyListAdapter=新的historyListAdapter(historyList);
historyList=新的ArrayList();
致:

historyList=newarraylist();
historyListAdapter=新的historyListAdapter(historyList);

将错误更改为java.lang.RuntimeException:无法反序列化对象。类com.maps.contour.contour.HistoryModel未定义无参数构造函数。如果您使用的是ProGuard,请确保这些构造函数在android.os.Handler.handleCallback(handle.java:754)在android.os.Handler.dispatchMessage(Handler.java:95)在android.os.Looper.loop(Looper.java:163)在android.app.ActivityThread.main(ActivityThread.java:6228)上没有剥离在java.lang.reflect.Method.invoke(本机方法)修复了错误,谢谢,只需要添加一个没有参数的构造函数。我发帖子之前没读过。谢谢你的帮助!将错误更改为java.lang.RuntimeException:无法反序列化对象。类com.maps.contour.contour.HistoryModel未定义无参数构造函数。如果您使用的是ProGuard,请确保这些构造函数在android.os.Handler.handleCallback(handle.java:754)在android.os.Handler.dispatchMessage(Handler.java:95)在android.os.Looper.loop(Looper.java:163)在android.app.ActivityThread.main(ActivityThread.java:6228)上没有剥离在java.lang.reflect.Method.invoke(本机方法)修复了错误,谢谢,只需要添加一个没有参数的构造函数。我发帖子之前没读过。谢谢你的帮助!
    historyList = new ArrayList<>();
    historyListAdapter = new HistoryListAdapter(historyList);