我是初学者应用程序开发试图创建聊天应用程序。该应用程序启动顺利,但当我想显示在一个回收查看应用程序崩溃的所有注册用户。它没有显示任何错误,但应用程序仍然崩溃,当注解掉错误时,只会转到下一行。任何帮助都将不胜感激。
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 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>
1条答案
按热度按时间rt4zxlrg1#
这里的问题是您尚未设置内容视图。
在oncreate方法中添加此行:-
setContentView(R.layout.your_activity_xml);
在你的super.oncreate
被称为。如果您不分配内容视图,则不会确定从何处获取回收视图:-
nuserlist = (RecyclerView) findViewById(R.id.userlist);
.