Android Studio - (java.lang.NullPointerException: 尝试调用虚拟方法 'com.google.firebase.database.ChildEventL

Posted

技术标签:

【中文标题】Android Studio - (java.lang.NullPointerException: 尝试调用虚拟方法 \'com.google.firebase.database.ChildEventListener\')【英文标题】:Android Studio - (java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener')Android Studio - (java.lang.NullPointerException: 尝试调用虚拟方法 'com.google.firebase.database.ChildEventListener') 【发布时间】:2021-06-03 11:02:46 【问题描述】:

我正在尝试从 Firebase 实时数据库中检索数据以显示在 android Studio 的 RecyclerView 中,但是在模拟器上运行应用程序后,它只显示了 1 行数据,所以我检查了 logcat,这是错误

java.lang.RuntimeException:无法启动活动 组件信息com.example.test/com.example.test.MainActivity: java.lang.NullPointerException:尝试调用虚拟方法 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' 在空对象引用上

我现在只是尝试检索小贩的名称和描述

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

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

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;

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 ComponentInfocom.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

【问题讨论】:

请编辑您的问题并在查询对象上添加您使用addChildEventListener() 的代码。 【参考方案1】:

我认为您需要检查您的 Hawker 类是否与 firebase 中的数据匹配,您的 java 类中的属性名称和属性数量应与 firebase 匹配

【讨论】:

以上是关于Android Studio - (java.lang.NullPointerException: 尝试调用虚拟方法 'com.google.firebase.database.ChildEventL的主要内容,如果未能解决你的问题,请参考以下文章

android studio怎么更新

android studio setting 在哪

如何解决android studio2.3导入android studio2.1的问题

Android studio上找不到setting?

android studio v4包冲突

如何用android studio 编成app