在使用 Recycler View 时尝试在空对象引用上调用虚拟方法
Posted
技术标签:
【中文标题】在使用 Recycler View 时尝试在空对象引用上调用虚拟方法【英文标题】:attempt to invoke virtual method on null object reference while using Recycler View 【发布时间】:2021-03-05 20:25:22 【问题描述】:我是初学者应用开发者,正在尝试创建聊天应用。该应用程序启动顺利,但是当我想在 Recycler View 应用程序中显示所有注册用户时会崩溃。它仍然没有显示任何错误,应用程序崩溃,当注释掉错误时,错误只是转到下一行。任何帮助将不胜感激。
Logcat
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 ComponentInfocom.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_
android:layout_
tools:context=".AllUsers">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/userlist"
android:layout_
android:layout_ />
</RelativeLayout>
【问题讨论】:
我猜你需要像这样设置你的内容视图:-setContentView(R.layout.your_activity_xml);
在你的 super.onCreate
方法之后。由于缺少此行,因此您的 recylerview 永远不会分配给任何东西,因为它会给出空指针异常
@AppDev。 thnx 人它工作了
【参考方案1】:
这里的问题是你没有设置你的内容视图。
在你的 onCreate 方法中添加这一行:-
setContentView(R.layout.your_activity_xml);
在您的super.oncreate
被调用之后。
如果您不分配内容视图,则无法识别从何处获取您的 recyclerView :- nuserlist = (RecyclerView) findViewById(R.id.userlist);
。
【讨论】:
简单来说,Activity 没有要显示的布局。以上是关于在使用 Recycler View 时尝试在空对象引用上调用虚拟方法的主要内容,如果未能解决你的问题,请参考以下文章
尝试在空对象引用上调用虚拟方法 'void android.view.View.measure(int, int)'
尝试在空对象引用上调用虚拟方法 'android.view.Window$Callback android.view.Window.getCallback()'
Android Studio:尝试在空对象引用上调用虚拟方法“void android.view.View.setOnClickListener”
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.view.View.getImportantForAccessibility()”
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.view.View.getBottom()”
尝试在空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)”