从 firebase 获取名称时 model_user 中获取 null - AndroidX

当我在 MainActivity 中时,当使用 model_user 从 firebase 设置用户名时,我得到 null,这是我使用的代码


//MainActivity.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

tools:context=".MainActivity">


<com.google.android.material.appbar.AppBarLayout

    android:layout_width="match_parent"

    android:layout_height="wrap_content">


    <androidx.appcompat.widget.Toolbar

        android:id="@+id/Toolbar_Main"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="@color/colorPrimaryDark"

        android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar">


        <de.hdodenhof.circleimageview.CircleImageView

            android:layout_width="30dp"

            android:layout_height="30dp"

            android:id="@+id/profile_image"

            android:src="@drawable/profile_blank"/>




        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:id="@+id/username"

            android:textSize="18sp"

            android:layout_marginLeft="25dp"

            android:textColor="#fff"

            android:textStyle="bold"

            android:layout_marginStart="25dp" />


    </androidx.appcompat.widget.Toolbar>


    <com.google.android.material.tabs.TabLayout

        android:id="@+id/tab_layout"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="@color/colorPrimaryDark"

        app:tabSelectedTextColor="#fff"

        app:tabIndicatorColor="#fff"

        app:tabTextColor="#fff"/>


</com.google.android.material.appbar.AppBarLayout>


<androidx.viewpager.widget.ViewPager

    android:id="@+id/view_pager"

    android:layout_width="match_parent"

    android:layout_height="match_parent"


</LinearLayout>


qq_花开花谢_0
浏览 112回答 2
2回答

慕工程0101907

删除 getter-setter 并添加空构造函数ModelUser.java&nbsp; public class ModelUser {&nbsp; &nbsp; public String id;&nbsp; &nbsp; public String username;&nbsp; &nbsp; public String imageURL;&nbsp; &nbsp; public ModelUser(String id, String username, String imageURL) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; this.username = username;&nbsp; &nbsp; &nbsp; &nbsp; this.imageURL = imageURL;&nbsp; &nbsp; }&nbsp; &nbsp; public ModelUser() {&nbsp; &nbsp; }&nbsp; &nbsp; }尝试这个

慕运维8079593

它不需要删除 setter getter,那么从此public class ModelUser {private String id;private String username;private String imageURL;public ModelUser(String id, String username, String imageURL) {&nbsp; &nbsp; this.id = id;&nbsp; &nbsp; this.username = username;&nbsp; &nbsp; this.imageURL = imageURL;}public String getId() {&nbsp; &nbsp; return id;}public void setId(String id) {&nbsp; &nbsp; this.id = id;}public String getUsername() {&nbsp; &nbsp; return username;}public void setUsername(String username) {&nbsp; &nbsp; this.username = username;}public String getImageURL() {&nbsp; &nbsp; return imageURL;}public void setImageURL(String imageURL) {&nbsp; &nbsp; this.imageURL = imageURL;}}只需添加一个空的构造函数即可正常工作:)public class ModelUser {public String id;public String username;public String imageURL;public ModelUser(String id, String username, String imageURL) {&nbsp; &nbsp; this.id = id;&nbsp; &nbsp; this.username = username;&nbsp; &nbsp; this.imageURL = imageURL;}//Add thispublic ModelUser() {}public String getId() {&nbsp; &nbsp; return id;}public void setId(String id) {&nbsp; &nbsp; this.id = id;}public String getUsername() {&nbsp; &nbsp; return username;}public void setUsername(String username) {&nbsp; &nbsp; this.username = username;}public String getImageURL() {&nbsp; &nbsp; return imageURL;}public void setImageURL(String imageURL) {&nbsp; &nbsp; this.imageURL = imageURL;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java