Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 用户列表为空_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Android 用户列表为空

Android 用户列表为空,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我想使用Firebase实时数据库创建一个聊天应用程序,其中包含一个RecyclerView和一个CardView,每个用户都有自己的名字。问题是,只有当数据库中有3个人时,才会显示CardView。 我在此链接中附上了我的数据库图片: 用户片段: public class UsersFragment extends Fragment { private RecyclerView recyclerView; private UserAdapter userAdapter;

我想使用Firebase实时数据库创建一个聊天应用程序,其中包含一个RecyclerView和一个CardView,每个用户都有自己的名字。问题是,只有当数据库中有3个人时,才会显示CardView。 我在此链接中附上了我的数据库图片:

用户片段:

public class UsersFragment extends Fragment {
    private RecyclerView recyclerView;
    private UserAdapter userAdapter;
    private List<User> mUsers;


    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate ( R.layout.fragment_users3, container, false );
recyclerView=view.findViewById (R.id.recycler_view  );

recyclerView.setLayoutManager ( new LinearLayoutManager ( getContext () ) );
mUsers=new ArrayList<> (  );
readUsers();

        return view;
    }

    private void readUsers () {
        final FirebaseUser firebaseUser= FirebaseAuth.getInstance ().getCurrentUser ();
        DatabaseReference referenc= FirebaseDatabase.getInstance ().getReference ("users");

        referenc.addValueEventListener ( new ValueEventListener () {
            @Override
            public void onDataChange (@NonNull DataSnapshot dataSnapshot) {
                mUsers.clear ();
                for(DataSnapshot snapshot:dataSnapshot.getChildren ()){
                    User user=snapshot.getValue (User.class);
                    assert user!=null;
                    assert firebaseUser!=null;
                    if(!firebaseUser.getUid ().equals ( user.getUid () )){
                        mUsers.add ( user );

                    }
                }
                userAdapter=new UserAdapter ( getContext (),mUsers );
                recyclerView.setAdapter ( userAdapter );
            }

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

            }
        } );
    }


}
还有我的TabLayout和ViewPager所在的活动

public class UsersActivity extends AppCompatActivity {


    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_users );
        TabLayout tablayout=findViewById ( R.id.tab_layout );
        ViewPager viewPager=findViewById ( R.id.view_pager );
        ViewPagerAdapter viewPagerAdapter=new ViewPagerAdapter ( getSupportFragmentManager () );
        viewPagerAdapter.addFragment ( new ChatFragment (),"Chats" );  // `new ChatFragment()` should be inside `FragmentPagerAdapter.getItem()`
        viewPagerAdapter.addFragment ( new UsersFragment (),"Users" );  // `new UsersFragment()` should be inside `FragmentPagerAdapter.getItem()`
        viewPager.setAdapter (viewPagerAdapter  );
        tablayout.setupWithViewPager (viewPager  );

    }
    class ViewPagerAdapter extends FragmentPagerAdapter{
private ArrayList<Fragment> fragments; // this line can cause crashes
private ArrayList<String> titles;
ViewPagerAdapter(FragmentManager fn){
    super(fn);
    this.fragments=new ArrayList<> (  );
    this.titles=new ArrayList<> (  );
}
        @NonNull
        @Override
        public Fragment getItem (int position) {
            return fragments.get ( position );
        }

        @Override
        public int getCount () {
            return fragments.size ();
        }
        public void addFragment(Fragment fragment,String title){
    fragments.add ( fragment ); // this line can cause crashes
    titles.add ( title );
        }

        @Nullable
        @Override
        public CharSequence getPageTitle (int position) {
            return titles.get ( position );
        }
    }
}


我希望显示数据库中的所有用户。

将firebase数据库中的键更改为firstName、secondName、email,而不是firstName、secondName和电子邮件。如果在模型类中找不到正确的键及其getter和setter,则无法映射firebase中的数据。

将firebase数据库中的键更改为firstName、secondName、email,而不是firstName、secondName和E-mail。如果在模型类中找不到正确的键及其getter和setter,则无法映射firebase中的数据。

数据库中的JSON中有属性
FirstName
。在
User
类中,您有一个方法
getFirstName()
,该方法(根据Firebase使用的JavaBean约定)对应于属性
firstName
。由于情况不同,这是一个不同的属性

要使JSON和类匹配,可以将JSON更改为使用JavaBean属性名:

{
  "users": {
    "uid1": {
      "email": "...",
      "firstName": "...",
      "secondName": "..."
    }
  }
}
或者,您可以使用
PropertyName
属性使Java类与当前JSON匹配:

public class User {
    private String firstName;
    private String secondName;
    private String uid;
    private String email;

    public User(){
    }

    public User (String firstName, String secondName, String uid, String email) {
        this.firstName = firstName;
        this.secondName = secondName;
        this.uid=uid;
        this.email=email;
    }

    public User (String firstname, String secondname) {
    }

    @PropertyName("FirstName")
    public String getFirstName () {
        return firstName;
    }

    @PropertyName("SecondName")
    public String getSecondName () {
        return secondName;
    }

    @PropertyName("Uid")
    public String getUid () {
        return uid;
    }

    @PropertyName("E-mail")
    public String getEmail () {
        return email;
    }

    @PropertyName("FirstName")
    public void setFirstName (String firstName) {
        this.firstName = firstName;
    }

    @PropertyName("SecondName")
    public void setSecondName (String secondName) {
        this.secondName = secondName;
    }

    @PropertyName("Uid")
    public void setUid (String uid) {
        this.uid = uid;
    }

    @PropertyName("E-mail")
    public void setEmail (String email) {
        this.email = email;
    }
}

也可以在这里看到我的答案:

在数据库的JSON中有一个属性
FirstName
。在
User
类中,您有一个方法
getFirstName()
,该方法(根据Firebase使用的JavaBean约定)对应于属性
firstName
。由于情况不同,这是一个不同的属性

要使JSON和类匹配,可以将JSON更改为使用JavaBean属性名:

{
  "users": {
    "uid1": {
      "email": "...",
      "firstName": "...",
      "secondName": "..."
    }
  }
}
或者,您可以使用
PropertyName
属性使Java类与当前JSON匹配:

public class User {
    private String firstName;
    private String secondName;
    private String uid;
    private String email;

    public User(){
    }

    public User (String firstName, String secondName, String uid, String email) {
        this.firstName = firstName;
        this.secondName = secondName;
        this.uid=uid;
        this.email=email;
    }

    public User (String firstname, String secondname) {
    }

    @PropertyName("FirstName")
    public String getFirstName () {
        return firstName;
    }

    @PropertyName("SecondName")
    public String getSecondName () {
        return secondName;
    }

    @PropertyName("Uid")
    public String getUid () {
        return uid;
    }

    @PropertyName("E-mail")
    public String getEmail () {
        return email;
    }

    @PropertyName("FirstName")
    public void setFirstName (String firstName) {
        this.firstName = firstName;
    }

    @PropertyName("SecondName")
    public void setSecondName (String secondName) {
        this.secondName = secondName;
    }

    @PropertyName("Uid")
    public void setUid (String uid) {
        this.uid = uid;
    }

    @PropertyName("E-mail")
    public void setEmail (String email) {
        this.email = email;
    }
}

也可以在这里看到我的答案:

我按照你告诉我的做了,在调试器中它显示了“com.example.sportsbuddy.Fragments.UsersFragment$1.onDataChange(UsersFragment.java:60)”和我的应用程序崩溃。我能做什么?如果你的应用程序崩溃,找到完整的错误消息和堆栈跟踪,并将它们添加到这里或你的问题中(因为你可以将它们格式化以使其更具可读性)。顺便说一句:我给了你两个解决问题的选项。知道你使用这两个选项中的哪一个可能会有所帮助。我还在你的另一篇文章中更新了我对这个问题的答案。我按照你告诉我的做了,并在调试器中显示了我“在com.example.sportsbuddy.Fragments.UsersFragment$1.onDataChange(UsersFragment.java:60)”和我的应用程序崩溃。我能做什么?如果你的应用程序崩溃,找到完整的错误消息和堆栈跟踪,并将它们添加到这里,或者添加到你的问题中(因为你可以在那里对它们进行格式化以使其更具可读性)顺便说一句:我给了你两个解决问题的方法。知道你用哪一个可能会有帮助。我还在你的另一篇文章中更新了我对这个问题的回答。
public class User {
    private String firstName;
    private String secondName;
    private String uid;
    private String email;

    public User(){
    }

    public User (String firstName, String secondName, String uid, String email) {
        this.firstName = firstName;
        this.secondName = secondName;
        this.uid=uid;
        this.email=email;
    }

    public User (String firstname, String secondname) {
    }

    @PropertyName("FirstName")
    public String getFirstName () {
        return firstName;
    }

    @PropertyName("SecondName")
    public String getSecondName () {
        return secondName;
    }

    @PropertyName("Uid")
    public String getUid () {
        return uid;
    }

    @PropertyName("E-mail")
    public String getEmail () {
        return email;
    }

    @PropertyName("FirstName")
    public void setFirstName (String firstName) {
        this.firstName = firstName;
    }

    @PropertyName("SecondName")
    public void setSecondName (String secondName) {
        this.secondName = secondName;
    }

    @PropertyName("Uid")
    public void setUid (String uid) {
        this.uid = uid;
    }

    @PropertyName("E-mail")
    public void setEmail (String email) {
        this.email = email;
    }
}