尝试在空对象引用上调用虚拟方法'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)'

时间:2019-12-14 13:28:16

标签: java android nullpointerexception

当我尝试运行代码时,它显示一个错误,即在空对象引用上的空对象引用上出现RecyclerView.setHasFixedSize(boolean)'。 Android Studio在RecyclerView.setHasFixedSize(true)行中检测到错误

android studio检测到的问题在此行recyclerView.setHasFixedSize(true);。

这是活动

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class YourPoetryActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    FirebaseDatabase mFirebaseDatabase;
    DatabaseReference mRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_your_poetry);

        recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setHasFixedSize(true);


        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        mFirebaseDatabase = FirebaseDatabase.getInstance();
        mRef = mFirebaseDatabase.getReference("Poetry");

    }
    @Override
    protected void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
                new FirebaseRecyclerAdapter<Model, ViewHolder>(
                        Model.class,
                        R.layout.row,
                        ViewHolder.class,
                        mRef
                ) {
                    @Override
                    protected void populateViewHolder(ViewHolder viewHolder, Model model, int i) {

                        viewHolder.setDetails(getApplicationContext(), model.getPoetryLine1(), model.getPoetryLine2(), model.getPoetName());
                    }
                };

        recyclerView.setAdapter(firebaseRecyclerAdapter);

    }
}

这是适配器

package com.marsad.poetryapp;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class ViewHolder extends RecyclerView.ViewHolder {

    View mView;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        mView = itemView;

    }

    public void setDetails(Context ctx, String poetryLine1, String poetryLine2, String poetName){

        TextView mPoetryLine1 = mView.findViewById(R.id.poetryLine1);
        TextView mPoetryLine2 = mView.findViewById(R.id.poetryLine2);
        TextView mPoetName = mView.findViewById(R.id.poetName);

        mPoetryLine1.setText(poetryLine1);
        mPoetryLine2.setText(poetryLine2);
        mPoetName.setText(poetName);
    }
}

logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.marsad.poetryapp/com.marsad.poetryapp.YourPoetryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2890)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2968)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:6710)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:550)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
        at com.marsad.poetryapp.YourPoetryActivity.onCreate(YourPoetryActivity.java:25)
        at android.app.Activity.performCreate(Activity.java:7181)
        at android.app.Activity.performCreate(Activity.java:7172)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2968) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:192) 
        at android.app.ActivityThread.main(ActivityThread.java:6710) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:550) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826) 
2019-12-14 18:12:56.655 23529-23529/? I/Process: Sending signal. PID: 23529 SIG: 9

1 个答案:

答案 0 :(得分:0)

对于您的活动,您应该有一个布局文件,其中包含ID为“ recyclerview”的RecyclerView。