[英]Android Studio - (java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener')
I am trying to retrieve data from Firebase real time db to display in RecyclerView of Android Studio, however after running the app on emulator, it only managed to display 1 row of data so i checked the logcat and this is the error我正在尝试从 Firebase 实时数据库中检索数据以显示在 Android Studio 的 RecyclerView 中,但是在模拟器上运行该应用程序后,它只能显示 1 行数据,所以我检查了 logcat,这是错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com. google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
I am only attempting to retrieve the hawker's name and description for now我现在只是试图检索小贩的名称和描述
MainActivity.java MainActivity.java
public class MainActivity extends AppCompatActivity {
DatabaseReference ref;
private FirebaseRecyclerOptions<Hawker> options;
private FirebaseRecyclerAdapter<Hawker, myViewHolder> adapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ref = FirebaseDatabase.getInstance().getReference().child("hawkers");
recyclerView=findViewById(R.id.recycler);
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
options = new FirebaseRecyclerOptions.Builder<Hawker>().setQuery(ref, Hawker.class).build();
adapter = new FirebaseRecyclerAdapter<Hawker, myViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull Hawker model) {
holder.textViewID.setText(model.getName());
holder.textViewName.setText(model.getDescription());
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_view_layout,parent,false);
return new myViewHolder(v);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}
}
myViewHolder.java myViewHolder.java
public class myViewHolder extends RecyclerView.ViewHolder{
TextView textViewID, textViewName;
public myViewHolder(@NonNull View itemView) {
super(itemView);
textViewID = itemView.findViewById(R.id.textViewID);
textViewName = itemView.findViewById(R.id.textViewName);
}
}
Hawker.java豪客.java
public class Hawker {
private String name;
private String description;
public Hawker() {
}
public Hawker(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
} }
the full error in logcat logcat 中的完整错误
2021-03-05 00:24:30.691 6466-6466/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 6466
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
at com.firebase.ui.database.FirebaseArray.onCreate(FirebaseArray.java:54)
at com.firebase.ui.common.BaseObservableSnapshotArray.addChangeEventListener(BaseObservableSnapshotArray.java:97)
at com.firebase.ui.database.FirebaseRecyclerAdapter.startListening(FirebaseRecyclerAdapter.java:52)
at com.example.test.MainActivity.onCreate(MainActivity.java:67)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-03-05 00:24:30.703 6466-6466/? I/Process: Sending signal. PID: 6466 SIG: 9
Output in emulator These are the data in firebase模拟器中的 Output 这些是 firebase 中的数据
i think you need to check whether your Hawker class is matched with the data in firebase or not, the attribute name and number of attributes in your java class should match to the firebase i think you need to check whether your Hawker class is matched with the data in firebase or not, the attribute name and number of attributes in your java class should match to the firebase
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.