onDataChange()未调用

时间:2019-06-24 07:28:46

标签: firebase android-studio firebase-realtime-database

未调用onDataChange()函数从firebase检索数据。同一段代码似乎可以在其他活动中使用。

我已经尝试过在这里有关于stackoverflow类似问题的人的建议,但是它们似乎没有用或不适合我的情况。

public void getDetails()
    {
        mFirebaseInstance = FirebaseDatabase.getInstance();
        mFirebaseDatabase = mFirebaseInstance.getReference("SDP Names/SCIT").child("First Year");

        ValueEventListener valueEventListener=new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()){
                    name2 = ds.child("Name").getValue(String.class);
                    Log.d("TAG", name2+ "/");
                    nameList.add(name2);
                }
                nameArr=new String[nameList.size()];
                nameArr=nameList.toArray(nameArr);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        }; 

    mFirebaseDatabase.addListenerForSingleValueEvent(valueEventListener);
}

这是错误消息:

  

E / Android运行时:致命异常:主要       流程:com.revauniversity.revasdpapp,PID:15752       java.lang.NullPointerException:尝试在空对象上调用虚拟方法“ java.lang.String java.lang.Object.toString()”   参考           在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:453)           在android.widget.ArrayAdapter.getView(ArrayAdapter.java:415)           在android.widget.AbsListView.obtainView(AbsListView.java:2399)           在android.widget.ListView.measureHeightOfChildren(ListView.java:1482)           在android.widget.ListView.onMeasure(ListView.java:1389)           在android.view.View.measure(View.java:23200)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)           在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)           在android.view.View.measure(View.java:23200)           在android.support.v7.widget.AlertDialogLayout.tryOnMeasure(AlertDialogLayout.java:134)           在android.support.v7.widget.AlertDialogLayout.onMeasure(AlertDialogLayout.java:64)           在android.view.View.measure(View.java:23200)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)           在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)           在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)           在android.view.View.measure(View.java:23200)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)           在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)           在android.view.View.measure(View.java:23200)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)           在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)           在android.view.View.measure(View.java:23200)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)           在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)           在android.widget.LinearLayout.measureVertical(LinearLayout.java:825)           在android.widget.LinearLayout.onMeasure(LinearLayout.java:704)           在android.view.View.measure(View.java:23200)           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)           在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)           在com.android.internal.policy.DecorView.onMeasure(DecorView.java:720)           在android.view.View.measure(View.java:23200)           在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2766)           在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1590)           在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1901)           在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1505)           在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:7354)           在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:1092)           在android.view.Choreographer.doCallbacks(Choreographer.java:888)           在android.view.Choreographer.doFrame(Choreographer.java:819)           在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:1078)           在android.os.Handler.handleCallback(Handler.java:873)           在android.os.Handler.dispatchMessage(Handler.java:99)           在android.os.Looper.loop(Looper.java:201)           在android.app.ActivityThread.main(ActivityThread.java:6823)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:547)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

我希望onDataChange()中的Log.d能够正常运行,以便指示数据正在复制到我的nameArr数组中。

0 个答案:

没有答案