无法将java.util.HashMap类型的值转换为String和app崩溃

时间:2018-05-06 19:48:21

标签: android firebase firebase-realtime-database

public class MainActivity extends AppCompatActivity {


    //points to the root node of the database
    DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference mConditionRef = mRootRef.child("condition");

    TextView content;
    Button sunny, foggy;


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

        content = (TextView) findViewById(R.id.condition);
        sunny = (Button) findViewById(R.id.sunny);
        foggy = (Button) findViewById(R.id.foggy);


    }

    @Override
    protected void onStart() {
        super.onStart();
        mConditionRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String text = dataSnapshot.getValue(String.class);
                content.setText(text);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


        sunny.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mConditionRef.setValue("Sunny");

                mRootRef.child("condition").push().setValue("Abhijeet Jain2");
            }
        });

        foggy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mConditionRef.setValue("Foggy");
            }
        });
    }
}

字符串会在实时数据库中添加一个自动生成的ID,但只要按下按钮"晴天"应用程序就会崩溃。 可能是什么原因和解决方案?

0 个答案:

没有答案