java - 尝试从 Firebase 检索数据并收到此错误 : SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

标签 java android firebase firebase-realtime-database

The database

我构建了一个应用程序,并尝试从 Firebase 检索用户信息。在注册表单之后,我有一个新 Activity ,用户需要编写他们的个人信息,并且该信息存储在 firebase 中。然后,在我的抽屉导航中,我有一个名为“AccountActivity”的 Activity ,用户可以在其中看到保存到数据库中的个人信息。我尝试编写一些代码来执行此操作,但是当我尝试访问 AccountActivity 时,它是空的..不从 firebase 检索用户信息,并且收到此错误:

SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero-length

您知道出了什么问题以及我可以做什么来修复它吗?

这是我在 firebase 中的数据,采用 JSON 格式,因此数据库不为空:

  "User" : {
    "cgIW3VyfDONZjgA0r8LfGNr1zEO2" : {
      "age" : "30",
      "gender" : "Male",
      "height" : "170",
      "username" : "Alex",
      "weight" : "80"
    },
    "gLMP5YpLheQBWGxfyT5xcfOI9ii1" : {
      "age" : "28",
      "height" : "155",
      "username" : "Ana",
      "weight" : "60"
    }
  }
}

用户类别:


public class User {

    public String username;
    public String age;
    public String weight;
    public String height;
    public String gender;

    public User(){

    }

    public User(String username, String age, String weight, String height, String gender){

        this.username = username;
        this.age = age;
        this.weight = weight;
        this.height = height;
        this.gender = gender;

    }

    public String getUsername() {
        return username;
    }

    public String getAge() {
        return age;
    }

    public String getWeight() {
        return weight;
    }

    public String getHeight() {
        return height;
    }

    public String getGender() {
        return gender;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}

帐户 Activity :

public class AccountActivity extends AppCompatActivity {

    ListView myListView;
    ArrayAdapter<String> arrayAdapter;
    DatabaseReference databaseReference;
    FirebaseUser user;
    List<String> itemList;
    String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account);

        myListView = findViewById(R.id.listView);
        user = FirebaseAuth.getInstance().getCurrentUser();
        userId = user.getUid();
        itemList = new ArrayList<>();

         databaseReference = FirebaseDatabase.getInstance().getReference();
        userRef = databaseReference.child("User");
        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    User user = ds.getValue(User.class);

                    itemList.add(user.getUsername());
                    itemList.add(user.getAge());
                    itemList.add(user.getWeight());
                    itemList.add(user.getHeight());
                    itemList.add(user.getGender());
                }
                arrayAdapter = new ArrayAdapter<>(AccountActivity.this, android.R.layout.simple_list_item_1, itemList);
                myListView.setAdapter(arrayAdapter);
            }

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

            }
        };
        userRef.addListenerForSingleValueEvent(valueEventListener);

activity_account.xml:

            android:id="@+id/textView"
            android:layout_width="263dp"
            android:layout_height="46dp"
            android:text="@string/account_informations"
            android:textColor="#FFFFFF"
            android:textSize="30sp"
            android:textStyle="bold|italic"
            app:fontFamily="@font/alex_brush"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <TextView
        android:id="@+id/tvUsernameDb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="178dp"
        android:layout_marginLeft="178dp"
        android:layout_marginTop="73dp"
        android:layout_marginEnd="175dp"
        android:layout_marginRight="175dp"
        android:layout_marginBottom="539dp"
        android:text="@string/textviewUsernameDb"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout" />

    <TextView
        android:id="@+id/tvAgeDb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="180dp"
        android:layout_marginLeft="180dp"
        android:layout_marginTop="51dp"
        android:layout_marginEnd="173dp"
        android:layout_marginRight="173dp"
        android:layout_marginBottom="468dp"
        android:text="@string/textviewAgeDb"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvUsernameDb" />

    <TextView
        android:id="@+id/tvWeightDb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="180dp"
        android:layout_marginLeft="180dp"
        android:layout_marginTop="47dp"
        android:layout_marginEnd="173dp"
        android:layout_marginRight="173dp"
        android:layout_marginBottom="402dp"
        android:text="@string/textviewWeightDb"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvAgeDb" />

    <TextView
        android:id="@+id/tvHeightDb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="181dp"
        android:layout_marginLeft="181dp"
        android:layout_marginTop="57dp"
        android:layout_marginEnd="171dp"
        android:layout_marginRight="171dp"
        android:layout_marginBottom="325dp"
        android:text="@string/textviewHeightDb"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvWeightDb" />

    <TextView
        android:id="@+id/tvGenderDb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="182dp"
        android:layout_marginLeft="182dp"
        android:layout_marginTop="54dp"
        android:layout_marginEnd="171dp"
        android:layout_marginRight="171dp"
        android:layout_marginBottom="252dp"
        android:text="@string/textviewGenderDb"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvHeightDb" />

在 XML 文件中,我放置了一些 TextView ,但没有文本,只有黑色空间,在 string.xml 中,我为每个 TextView 表单 Activity_account.xml 提供了类似的内容: <string name="textviewGenderDb">" "</string>

所以我的问题是:我的XML文件没有写错,还是代码写错了?我不知道问题出在哪里以及为什么会出现此错误。感谢您的帮助!

最佳答案

I did something wrong in the XML file, or the code is written wrong?

主要存在三个问题。

  1. User 类中的字段是公共(public)的。如果您想使用 getter 和 setter,请将它们全部设置为私有(private)

  2. 您的databaseReference 指向数据库的根目录,但用户会添加到User 节点中。因此,为了获取所有用户,应添加对 .child("User") 的调用。因此,请检查以下代码行:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference userRef = rootRef.child("User");
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                User user = ds.getValue(User.class);
                Log.d("TAG", user.getUsername());
            }
        }
    
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
        }
    };
    userRef.addListenerForSingleValueEvent(valueEventListener);
    

logcat 中的结果将是:

Alex
Ana

有两个认为你应该注意。首先,您可以直接将数据库中的节点映射到 User 对象,无需创建新对象并设置值。其次,节点应该更可能被命名为 Usersusers 而不仅仅是 User,因为在该节点中存在多个用户,而不是一个用户。单人。

  • 因为您正在包含多个用户的节点上进行迭代,you should use instead of TextView object a ListView 。通过这种方式,您可以在一行中添加每个详细信息。
  • 关于java - 尝试从 Firebase 检索数据并收到此错误 : SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62280674/

    相关文章:

    java - 如何从 Firebase 检索数据并将其设置为图表的 string 和 int

    java - 我可以为父表的外键列定义不同的类型吗?

    java - 摘机时结束去电

    java - 在 Android 中执行 shell 命令时避免 "su"toast?

    android - Flutter ClassNotFoundException (java.lang.RuntimeException) 崩溃错误

    java - 从 Firestore 数据库检索 GeoPoint 数组添加 map 多边形

    javascript - 未捕获错误 : Reference. 推送失败:

    java - 如何处理 Quartz 中的 misfire

    java - 提示HINT_PASS_DISTINCT_THROUGH可将PageRequest的每页返回的实体数量降低到低于配置的页面大小(PostgreSQL)

    android - Firebase IOS UTM 安装跟踪