在ListView错误中显示Firebase子项

时间:2017-01-22 18:08:58

标签: android listview firebase firebase-realtime-database

我有我的基本Firebase应用程序,它涉及从我设计的嵌入式系统发送数据,嵌入式工作正常,但我无法将数据显示为数据库中的子条目。

应用程序将连接并写入Firebase,即JSON

{
  "users" : {
"-Kb6KXHoEjDtrT5KFAkk" : {
  "title" : "Hello"
},
"-Kb6KcJRJchZ6FdYQwE3" : {
  "title" : "Hello"
    }
  }
}

信息不会显示在列表视图中,我的应用程序不需要对象,只需要在数据库中显示子条目。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize Firebase Auth and Database Reference
    mFirebaseAuth = FirebaseAuth.getInstance();
    mFirebaseUser = mFirebaseAuth.getCurrentUser();
    mDatabase = FirebaseDatabase.getInstance().getReference();

    if (mFirebaseUser == null) {
        // Not logged in, launch the Log In activity
        loadLogInView();
    } else {
        mUserId = mFirebaseUser.getUid();

        // Set up ListView
        final ListView listView = (ListView) findViewById(R.id.listview);
        final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
        listView.setAdapter(adapter);

        Item item = new Item("Hello"); // testing that i can write values
        mDatabase.child("users").push().setValue(item); // Values can be written


        // Use Firebase to populate the list.
        mDatabase.child("users").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                adapter.add((String) dataSnapshot.child("users").getValue());
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                adapter.add((String) dataSnapshot.child("users").getValue());

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
                adapter.remove((String) dataSnapshot.child("users").getValue());
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

Firebase权限是公开读写

{
  "rules": {
        ".read": true,
        ".write": true,
            }
          }

我得到的错误是一个空对象引用

FATAL EXCEPTION: main
                                                                     Process: com.m.j.project.josephmay, PID: 6843
                                                                     java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                                         at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
                                                                         at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
                                                                         at android.widget.AbsListView.obtainView(AbsListView.java:2929)
                                                                         at android.widget.ListView.makeAndAddView(ListView.java:1945)
                                                                         at android.widget.ListView.fillDown(ListView.java:719)
                                                                         at android.widget.ListView.fillFromTop(ListView.java:785)
                                                                         at android.widget.ListView.layoutChildren(ListView.java:1721)
                                                                         at android.widget.AbsListView.onLayout(AbsListView.java:2723)
                                                                         at android.view.View.layout(View.java:17945)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                         at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
                                                                         at android.view.View.layout(View.java:17945)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                                                                         at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                                                                         at android.view.View.layout(View.java:17945)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                         at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:433)
                                                                         at android.view.View.layout(View.java:17945)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                                                                         at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                                                                         at android.view.View.layout(View.java:17945)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                         at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
                                                                         at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                         at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                         at android.view.View.layout(View.java:17945)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                                                                         at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                                                                         at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:3178)
                                                                         at android.view.View.layout(View.java:17945)
                                                                         at android.view.ViewGroup.layout(ViewGroup.java:5812)
                                                                         at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2716)
                                                                         at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2417)
                                                                         at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1487)
                                                                         at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7450)
                                                                         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
                                                                         at android.view.Choreographer.doCallbacks(Choreographer.java:695)
                                                                         at android.view.Choreographer.doFrame(Choreographer.java:631)
                                                                         at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:158)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

0 个答案:

没有答案