com.google.firebase.database.DatabaseException:无法将java.util.HashMap类型的值转换为String

时间:2017-03-27 19:01:04

标签: android

我尝试使用firebase的文档指南从我的数据库中记录一个简单的字符串但是我收到此错误:  com.google.firebase.database.DatabaseException:无法将java.util.HashMap类型的值转换为String

这是我的代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;



public class MainActivity extends AppCompatActivity {



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


        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("brands");

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String value = dataSnapshot.getValue(String.class);

                Log.d("TAG", "Value is: " + value);
//;
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });



    }



}

感谢您提供任何帮助!

logcat的:

03-27 18:58:12.593 574-574/com.ofir.motoinfo E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: com.ofir.motoinfo, PID: 574
                                                               com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String
                                                                   at com.google.android.gms.internal.zzbtg.zzaF(Unknown Source)
                                                                   at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
                                                                   at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                                                                   at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                   at com.ofir.motoinfo.MainActivity$1.onDataChange(MainActivity.java:33)
                                                                   at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
                                                                   at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
                                                                   at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
                                                                   at android.os.Handler.handleCallback(Handler.java:751)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

2 个答案:

答案 0 :(得分:0)

您没有提供logcat,但错误位于以下行:

String value = dataSnapshot.getValue(String.class);

您正在申请String,但Firebase中的节点包含HashMap。确保您位于正确的节点或数据格式正确。

答案 1 :(得分:0)

想出来,我需要将孩子添加到DatabaseReference myRef = database.getReference(),因为我正在引用整个数据库。