首页 > 解决方案 > 在使用 Recycler View 时尝试在空对象引用上调用虚拟方法

问题描述

我是尝试创建聊天应用程序的初学者应用程序开发人员。该应用程序启动顺利,但是当我想在 Recycler View 应用程序中显示所有注册用户时会崩溃。它仍然没有显示任何错误,应用程序崩溃,当注释掉错误时,错误只是转到下一行。任何帮助将非常感激。

日志猫

2020-11-23 10:38:39.697 4907-4907/com.example.decide_o E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.decide_o, PID: 4907
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.decide_o/com.example.decide_o.AllUsers}: 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:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
        at com.example.decide_o.AllUsers.onCreate(AllUsers.java:38)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

所有用户

public class AllUsers extends AppCompatActivity {
    private RecyclerView nuserlist;
    private DatabaseReference nUserDatabase;
    private LinearLayoutManager nlayoutmanager;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        nUserDatabase=FirebaseDatabase.getInstance().getReference().child("Users");
        nlayoutmanager=new LinearLayoutManager(this);
        nuserlist = (RecyclerView) findViewById(R.id.userlist);
        nuserlist.setHasFixedSize(true);
        nuserlist.setLayoutManager(nlayoutmanager);
    }

    @Override
    protected void onStart() {
        super.onStart();
       FirebaseRecyclerAdapter<Users,UsersViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
                Users.class,
                R.layout.single_users,
                UsersViewHolder.class,
                nUserDatabase
        ) {
            @Override
            protected void populateViewHolder(UsersViewHolder usersViewHolder, Users users, int i) {
                usersViewHolder.setName(users.getName());
            }
        };
        nuserlist.setAdapter(firebaseRecyclerAdapter);
    }
    public static class UsersViewHolder extends RecyclerView.ViewHolder{
        View nView;
        public UsersViewHolder(@NonNull View itemView) {
            super(itemView);
            nView=itemView;

        }

        public void setName(String name) {
            TextView nameview=(TextView)nView.findViewById(R.id.single_username);
            nameview.setText(name);
        }
    }
}

xml

<RelativeLayout 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=".AllUsers">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/userlist"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

标签: javaandroidandroid-studioandroid-recyclerviewfirebaseui

解决方案


这里的问题是你没有设置你的内容视图。

在你的 onCreate 方法中添加这一行:- setContentView(R.layout.your_activity_xml);

在你super.oncreate被调用之后。

如果您不分配您的内容视图,那么将无法确定从哪里获取您的 recyclerView :- nuserlist = (RecyclerView) findViewById(R.id.userlist);


推荐阅读