【问题标题】:java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.Stringjava.lang.ClassCastException:java.util.HashMap 无法转换为 java.lang.String
【发布时间】:2015-11-10 03:31:24
【问题描述】:

嘿,谁能帮我解决这个错误:

java.lang.ClassCastException:java.util.HashMap 无法转换为 java.lang.String 在 in.xyz.firebase.MainActivity$1.onDataChange

MainActivity.java

import java.lang.String;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; 
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;

public class MainActivity extends AppCompatActivity {

private Firebase mRef;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Firebase.setAndroidContext(this);
}

@Override
protected void onStart() {
    super.onStart();
    Button b1 = (Button) findViewById(R.id.buttonSunny);
    Button b2 = (Button) findViewById(R.id.buttonFoggy);
   final  TextView t1 = (TextView) findViewById(R.id.textView);

    mRef = new Firebase("https://docs-    examples.firebaseio.com/web/saving-data/fireblog/posts");

    mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String value = (String) dataSnapshot.getValue();
            t1.setText(value);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
}
}

有人知道如何解决这个问题吗?

【问题讨论】:

标签: java android firebase


【解决方案1】:

始终使用 POJO 而不是 HashMap :)

@Rahul Chaurasia 的回答很好!但我只是想扩展 how you return another class

假设你有一个Todo的类:

public class Todo {
  private String author;
  private String message;

  public Todo() {
    // empty default constructor, necessary for Firebase to be able to deserialize blog posts
  }

  public String getAuthor() {
    return author;
  }

  public String getMessage() {
    return message;
  }

}

由于这个类有一个空的构造函数,它可以被 Firebase 反序列化。

  // Text view
  final TextView t1 = (TextView) findViewById(R.id.textView);

  // Get a reference to our posts
  Firebase ref = new Firebase("<my-firebase-db>/todos/1");

  // Get the todos
  ref.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot snapshot) {
         Todo todo = snapshot.getValue(Todo.class);
         t1.setText(todo.getMessage());
      }

      @Override
      public void onCancelled(FirebaseError firebaseError) {
          System.out.println("The read failed: " + firebaseError.getMessage());
      }
  });

使用类而不是HashMap 提供了更好的类型安全性。而且,它利用了 Firebase SDK 的内置功能。

【讨论】:

    【解决方案2】:

    我猜你正在使用https://www.firebase.com/docs/java-api/javadoc/com/firebase/client/DataSnapshot.html API,而你的 getValue() 方法正在返回 Hashmap。

    你应该这样做 -

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            HashMap<String, Object> yourData = dataSnapshot.getValue();
            // now get the data from the hashmap and set it to the text view
    
        }
    

    正如文档中给出的,getValue 也可以返回不同的类型,因此您必须相应地处理它。

    【讨论】:

    • 这不起作用。即使在类型转换之后,我仍然得到另一个 HashMap 而不是 Object。
    猜你喜欢
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多