列表视图显示用户错误

时间:2018-03-28 18:09:26

标签: java android listview firebase

我希望在ListView的选项卡布局中显示数据库中的用户列表,但是尝试在空对象引用上调用虚方法'java.lang.String java.lang.Object.toString()'时出错。经过多次搜索后,我没有成功删除它。任何人都可以帮助我吗?

错误

  

03-28 23:26:06.652 25767-25767 / com.example.avinash.tablayoutcheck   E / AndroidRuntime:致命异常:主要                                                                                       处理:com.example.avinash.tablayoutcheck,PID:25767                                                                                       java.lang.NullPointerException:尝试调用虚方法   空对象上的'java.lang.String java.lang.Object.toString()'   参考                                                                                           在   android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)                                                                                           在android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)                                                                                           在android.widget.AbsListView.obtainView(AbsListView.java:2474)                                                                                           在android.widget.ListView.measureHeightOfChildren(ListView.java:1301)                                                                                           在android.widget.ListView.onMeasure(ListView.java:1203)                                                                                           在android.view.View.measure(View.java:19161)                                                                                           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6118)                                                                                           在   android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723)                                                                                           在android.widget.LinearLayout.measureVertical(LinearLayout.java:788)                                                                                           在android.widget.LinearLayout.onMeasure(LinearLayout.java:648)                                                                                           在android.view.View.measure(View.java:19161)                                                                                           在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1630)                                                                                           在android.view.View.measure(View.java:19161)                                                                                           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6118)                                                                                           在   android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)                                                                                           在   android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:91)                                                                                           在   android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1361)                                                                                           在   android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)                                                                                           在android.view.View.measure(View.java:19161)                                                                                           在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6118)                                                                                           在android.widget.FrameLayout.onMeasure(FrameLayout.java:223)                                                                                           在   android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)                                                                                           在android.view.View.measure(View.java:19161)

UserlistFragment.java

  public class UserlistFragment extends Fragment {
private static final String TAG = "UserList";
ListView usersList;
private DatabaseReference mDatabaseUser;
ArrayList<String> list=new ArrayList<>();
private DatabaseReference root = FirebaseDatabase.getInstance().getReference().getRoot();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.userlist_fragment,container,false);

    mDatabaseUser = FirebaseDatabase.getInstance().getReference("users");

    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    usersList = (ListView)getActivity().findViewById(R.id.usersList);

    final ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_dropdown_item_1line,list);
    usersList.setAdapter(adapter);

    mDatabaseUser.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
           /* list.add((String) dataSnapshot.getValue(String.class));*/

            User user = dataSnapshot.getValue(User.class);

            // Check for null
            if (user == null) {
                Log.e(TAG, "User data is null!");
                return;
            }
            list.add(user.username);

            adapter.notifyDataSetChanged();
        }

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

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

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

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}
}

userlist_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_height="match_parent">
<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/usersList"/>
 </LinearLayout>

Database Structure

0 个答案:

没有答案