DatabaseException:无法将java.Lang.String对象转换为类型

时间:2018-11-22 18:04:43

标签: java android firebase firebase-realtime-database

我正在尝试从Firebase实时数据库中检索数据,但是遇到此问题。

我怀疑由于我在DatabaseReference中的路径而收到此错误。我尝试了不同的途径,但似乎都没有。

这是我的数据库(生成的文本是用户ID)

enter image description here

我存储数据结构的类

public class ERDataStructure {
private String temperature;
private String humidity;

public ERDataStructure() {

}
    public ERDataStructure(String temperature, String humidity) {
        this.temperature = temperature;
        this.humidity = humidity;
    }

public String getTemperature(){

    return temperature;
}
public void setTemperature(String temperature){

    this.temperature = temperature;
}

public String getHumidity() {

    return humidity;
}
public void setHumidity(String humidity) {

    this.humidity = humidity;
}
}

我的ReadingScreen.java(MainActivity)

import ...


public class ReadingScreen extends AppCompatActivity {

//Firebase Initialization
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
private FirebaseDatabase mFirebaseDatabase;

private static final String TAG = "ReadingScreen";
ERDataStructure mData;
private TextView textViewTemperatureNumber, textViewHumidityNumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reading_screen);
    //Find Views
    textViewHumidityNumber = (TextView) findViewById(R.id.humidity_number);
    textViewTemperatureNumber = (TextView) findViewById(R.id.temperature_number);
    // Initiate Firebase
    mAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    FirebaseUser user = mAuth.getCurrentUser();
    String userID = user.getUid();

     // Non-working Reference, but code works
    //myRef = mFirebaseDatabase.getReference();
    //myRef = mFirebaseDatabase.getReference().child(userID);

     //Crashing Reference
      myRef = mFirebaseDatabase.getReference().child(userID).child("ERDataStructure");
    //myRef = mFirebaseDatabase.getReference("ERDataStructure");
    //
    retrieveData();
}
public void gotoMainMenu (View view)
{
    Intent intent = new Intent(ReadingScreen.this, MainMenu.class);
    startActivity(intent);
}
private void retrieveData(){
    myRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            ERDataStructure ds = dataSnapshot.getValue(ERDataStructure.class);
            textViewTemperatureNumber.setText("" +ds.getTemperature());
            textViewHumidityNumber.setText(""+ds.getHumidity());

        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            ERDataStructure ds = dataSnapshot.getValue(ERDataStructure.class);
            textViewTemperatureNumber.setText("" +ds.getTemperature());
            textViewHumidityNumber.setText(""+ds.getHumidity());
        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

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

        }
    });
    myRef.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            List<ERDataStructure> arraylist = new ArrayList<ERDataStructure>();
            if (dataSnapshot != null && dataSnapshot.getValue() != null){
                //FirebaseUser user = mAuth.getCurrentUser();
                //String userID = user.getUid();
                for (DataSnapshot a : dataSnapshot.getChildren()){

                    ERDataStructure dataStructure = new ERDataStructure();
                    dataStructure.setTemperature(a.getValue(ERDataStructure.class).getTemperature());
                    dataStructure.setHumidity(a.getValue(ERDataStructure.class).getHumidity());
                    arraylist.add(dataStructure);
                }
            }
        }

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

        }
    });
}
}

错误记录在

ERDataStructure ds = dataSnapshot.getValue(ERDataStructure.class);

编辑:将另一个级别添加到我的数据库后,它可以运行

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要使用ValueEventListener而不是ChildEventListener,因此将其更改为:

myRef = mFirebaseDatabase.getReference().child(userID).child("ERDataStructure");

myRef.addValueEventListener(new ValueEventListener() {
   @Override
public void onDataChange(DataSnapshot dataSnapshot) {
   ERDataStructure ds = dataSnapshot.getValue(ERDataStructure.class);
 }
   @Override
public void onCancelled(DatabaseError databaseError) {
   }
});

在这种情况下,您使用ValueEventListener,因为当您使用childEventListener时,您基本上是在此快照中循环并以humidity类型返回String的值。就像在dataSnapshot.getChildren()中的valueventlistener上循环一样。