【问题标题】:Cast dataSnapshot from Firebase to Integer failed将数据快照从 Firebase 转换为整数失败
【发布时间】:2019-03-07 11:19:23
【问题描述】:

我正在尝试从实时 Firebase 数据库中提取一个名为 scoreBase 的整数。我想从数据库中读取数据,而不是监听变化,这就是我使用 mDatabase.addListenerForSingleValueEvent 的原因。我不明白如何将其转换为 Integer,但出现此错误:

java.lang.ClassCastException:java.lang.Long 无法转换为 java.lang.Integer 在 com.example.root.exercicis.Signin$2$1.onDataChange(Signin.java:131) 在 com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@16.0.5:183) 在 com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.5:75) 在 com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.5:63) 在 com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.5:55) 在 android.os.Handler.handleCallback(Handler.java:751) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6119) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

这是代码:

 public void onLogin(View view) {
    email = userEditText.getText().toString();
    password = passwordEditText.getText().toString();
        mAuth.signInWithEmailAndPassword(email,password)
        .addOnCompleteListener(this,new OnCompleteListener<AuthResult>() {
           @Override
              public void onComplete (@NonNull Task < AuthResult > task) {
                  if (task.isSuccessful()) {
                     // Sign in success, update UI with the signed-in user's information
                      FirebaseUser user = mAuth.getCurrentUser();
                      mDatabase = FirebaseDatabase.getInstance().getReference().child("users").child(task.getResult().getUser().getUid()).child("score");
                      mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
                          @Override
                          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                              int scoreBase = (int) dataSnapshot.getValue();
                              Toast.makeText(Signin.this, Integer.toString(scoreBase), Toast.LENGTH_SHORT).show();
                              sharedPreferences.edit().putInt("SuperScore", scoreBase).apply();
                          }

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

                          }
                      });

                      updateUI(user);
                  } else {
                      // If sign in fails, display a message to the user.
                      Toast.makeText(Signin.this, "Authentication failed.",  Toast.LENGTH_SHORT).show();
                     updateUI(null);
        }

        // ...
    }
});
}

【问题讨论】:

  • 请添加您的数据库结构。

标签: android firebase firebase-realtime-database


【解决方案1】:

我找到了替换的解决方案

int scoreBase = (int) dataSnapshot.getValue();

为

int scoreBase = (int) dataSnapshot.getValue(Integer.class);

【讨论】:

  • 这确实是从快照中获取整数值的正确方法。我通常坚持只在我的 Java 代码中使用 Long 来处理来自 Firebase 的值,因为这是数据库中底层类型的最佳反映。
【解决方案2】:

从DataSnapShot.getValue() 的文档中,getValue() 返回的 Object 只能转换为 Boolean、String、Long、Double、Map、List(非整数)。排队

 int scoreBase = (int) dataSnapshot.getValue();

您将其直接转换为导致转换异常的 int。 您首先需要将其转换为 Long,然后转换为 int,例如:

int scoreBase = (int) ((long) dataSnapshot.getValue());

或者你也可以使用:

int scoreBase = (int) dataSnapshot.getValue(Integer.class);

您可以阅读文档here。

【讨论】:

    【解决方案3】:

    首先检查对象是否为空。

    你也可以试试。

    Integer.ValueOf(dataSnapShot.getValue());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      • 2018-04-02
      • 2016-10-28
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 2021-03-25
      相关资源
      最近更新 更多