简体   繁体   English

java.lang.ClassCastException:无法将java.util.HashMap强制转换为java.lang.String

[英]java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String

Hey can anyone help me out clearing this error : 嘿,有谁能帮助我清除此错误:

java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String at in.xyz.firebase.MainActivity$1.onDataChange java.lang.ClassCastException:无法将in.xyz.firebase.MainActivity $ 1.onDataChange上的java.util.HashMap强制转换为java.lang.String

MainActivity.java 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) {

        }
    });
}
}

Does anyone have any idea how to resolve this? 有谁知道如何解决这个问题?

Always use a POJO rather than a HashMap :) 始终使用POJO而不是HashMap :)

Great answer by @Rahul Chaurasia! @Rahul Chaurasia的一个很好的答案! But I just wanted to expand upon how you return another class . 但我只是想扩展一下您如何返回另一个班级

Let's say you have a class of Todo : 假设您有一类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;
  }

}

Since this class has an empty constructor it's eligible for deserialization with Firebase. 由于此类的构造函数为空,因此可以使用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());
      }
  });

Using a class rather than a HashMap provides much better type-safety. 使用类而不是HashMap可以提供更好的类型安全性。 And, it takes advantage of something that's just built-in to the Firebase SDK. 而且,它利用了Firebase SDK内置的功能。

I guess you are using https://www.firebase.com/docs/java-api/javadoc/com/firebase/client/DataSnapshot.html API and your getValue() method is returning Hashmap. 我猜您正在使用https://www.firebase.com/docs/java-api/javadoc/com/firebase/client/DataSnapshot.html API,并且您的getValue()方法返回了Hashmap。

You should do like this one - 你应该喜欢这个-

    @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

    }

Also as given in the documentation, getValue can return different type as well so you'll have to handle it accordingly. 另外,如文档中所给定的,getValue也可以返回不同的类型,因此您必须相应地处理它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:java.util.HashMap无法强制转换为java.lang.Comparable - java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.Comparable java.lang.ClassCastException:java.util.HashMap $ EntrySet无法转换为java.util.HashSet - java.lang.ClassCastException: java.util.HashMap$EntrySet cannot be cast to java.util.HashSet java.lang.ClassCastException: java.util.HashMap$EntrySet 不能转换为 java.util.Map$Entry - java.lang.ClassCastException: java.util.HashMap$EntrySet cannot be cast to java.util.Map$Entry java.lang.ClassCastException:无法将java.util.HashMap强制转换为com.jms.testing.spring.InstructionMessage - java.lang.ClassCastException: java.util.HashMap cannot be cast to com.jms.testing.spring.InstructionMessage java.lang.ClassCastException:java.util.HashMap无法转换为自定义数据类 - java.lang.ClassCastException: java.util.HashMap cannot be cast to custom data class java.lang.ClassCastException: class java.util.Z063A5BC470661C3C7909 无法转换 - java.lang.ClassCastException: class java.util.HashMap cannot be cast : SpringBoot Hashmap:java.lang.ClassCastException:java.lang.Long 无法转换为 java.lang.String - Hashmap: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String java.lang.ClassCastException:java.util.ArrayList无法强制转换为java.lang.String - java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String java.lang.ClassCastException:java.lang.String无法强制转换为java.util.Date - java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date java.lang.ClassCastException:无法将java.lang.String强制转换为java.util.Calendar - java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Calendar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM