Retreiving data from firebase

笑着哭i 提交于 2021-01-28 08:47:53

问题


Here is my android code for retrieving data from firebase real-time database:

package com.example.firebaseapp;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import`enter code here` android.view.View;
import android.widget.Button;
import android.widget.TextView;

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 {

    Button f;
    TextView status;

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        f=(Button)findViewById(R.id.button);
        status=(TextView) findViewById(R.id.lightstatus);

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

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError){
                status.setText("Could not fetch data..");
            }
        });
    }

    public void lighton(View view){
        myRef.setValue(1);
    }

    public void lightoff(View view){
        myRef.setValue(0);
    }
}

The app is compiling properly but whenever I am running it the app crashes with the error message "Unfortunately, Firebase App stopped". Please help me solve this problem. Here is a snippet from my database:


回答1:


Change String to Integer or Long as light value is not String.

public class MainActivity extends AppCompatActivity {

Button f;
TextView status;

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

    f = (Button)findViewById(R.id.button);
    status = (TextView) findViewById(R.id.lightstatus);

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

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Integer value = dataSnapshot.getValue(Integer.class);
            status.setText(String.valueOf(value));
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError){
            status.setText("Could not fetch data..");
        }
    });
}

public void lighton(View view){
    myRef.setValue(1);
}

public void lightoff(View view){
    myRef.setValue(0);
  }

}


来源:https://stackoverflow.com/questions/54862858/retreiving-data-from-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!