从Firebase实时数据库中读取应用程序崩溃

时间:2017-08-27 15:15:50

标签: android json firebase-realtime-database

我想阅读Firebase实时数据库中的所有元素(我通过导入.json文件填充(未使用我的应用程序填充)。所以我尝试了很多其他解决方案但是它们没有用。是我的实时数据库:

Real time database structure

以下是我如何阅读主要活动中的数据:

private int Question_no=0;
private Boolean Boolean_Var=false;

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

    String[] Question_Array = getResources().getStringArray(R.array.Question1);
    TextView Questions = (TextView) findViewById(R.id.Question);
    Questions.setText(Question_Array[Question_no]);

    String[] Radio_Button1_Array = getResources().getStringArray(R.array.Option_1);
    RadioButton Radio_Button1 = (RadioButton) findViewById(R.id.radioButton1);
    Radio_Button1.setText(Radio_Button1_Array[Question_no]);

    findViewById(R.id.MenuButton).setVisibility(View.INVISIBLE);
    findViewById(R.id.NextButton).setVisibility(View.INVISIBLE);

}
public void On_RadioButton1_Click(View view)
{
    if(Boolean_Var==false)
    {
        String[] CorrectAns_Array = getResources().getStringArray(R.array.Answer1);
        String CorrectAns = CorrectAns_Array[Question_no];
        String[] Answer_Array = getResources().getStringArray(R.array.Option_1);
        String Answer = Answer_Array[Question_no];


        if(Answer.equals(CorrectAns))
        {
            RadioButton Right_Ans = (RadioButton) findViewById(R.id.radioButton1);
            Right_Ans.setTextColor(Color.GREEN);
            AnswerSubmitted();
        }
        else
        {
            RadioButton Wrong_Ans = (RadioButton) findViewById(R.id.radioButton1);
            Wrong_Ans.setTextColor(Color.RED);
            GreenTick();
            AnswerSubmitted();
         }
    }
    Boolean_Var=true;

}
 public void AnswerSubmitted()
{
    findViewById(R.id.MenuButton).setVisibility(View.VISIBLE);
    findViewById(R.id.NextButton).setVisibility(View.VISIBLE);
}

这是我的停车课:

 public  final  ArrayList<Parking> parkingArrayList=new ArrayList<Parking>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
    firebaseDatabase.setPersistenceEnabled(true);
    DatabaseReference databaseReference=firebaseDatabase.getReference();
    databaseReference.child("Parking").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
           for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
            Parking p=dataSnapshot1.getValue(Parking.class);
           }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

我已将其与Firebase连接,安装google-services.json文件(通过使用firebase工具),但是当我启动应用程序时它崩溃(“不幸的是,najdiparking(我的应用名称)已经停止)。当我填写手动列表(不读取数据库)应用程序工作。 这是依赖项'com.google.firebase:firebase-database:10.0.1。

我还使用类中的名称检查我的.json文件名。

我的logcat(我希望这会对你有帮助)

public class Parking implements Comparable<Parking>,Parcelable { public String name; public String zone; public String location; public int capacity; public double latitude; public double longtitude; int free=0; int taken=0; int id; public Parking(){ } public Parking(int capacity,double latitude,String location, double longtitude,String name,String zone){ this.capacity=capacity; this.location=location; this.name=name; this.zone=zone; this.latitude=latitude; this.longtitude=longtitude; this.taken=getTaken(); this.free=getFree(); } protected Parking(Parcel in) { name = in.readString(); zone = in.readString(); location = in.readString(); capacity = in.readInt(); latitude = in.readDouble(); longtitude = in.readDouble(); free = in.readInt(); taken = in.readInt(); } public static final Creator<Parking> CREATOR = new Creator<Parking>() { @Override public Parking createFromParcel(Parcel in) { return new Parking(in); } @Override public Parking[] newArray(int size) { return new Parking[size]; } }; public int getCapacity() { return capacity; } public int getFree() { return capacity-getTaken(); } public int getTaken() { Random random=new Random(); this.taken=random.nextInt(capacity+1); return taken; } public void setFree(int taken){ free=capacity-taken; } public void setTaken(int taken) { Random random=new Random(); this.taken=random.nextInt(capacity+1); } public double getLatitude(){ return latitude; } public double getLongtidue(){ return longtitude; } @Override public String toString() { return String.format("%s\t\tZone: %s\nlocation: %s\nCapacity: %d\t\tFree: %d",name,zone,location,capacity,getFree()); } @Override public int compareTo(@NonNull Parking parking) { return Integer.compare(parking.free,this.free); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeInt(capacity); parcel.writeDouble(latitude); parcel.writeString(location); parcel.writeDouble(longtitude); parcel.writeString(name); parcel.writeString(zone); parcel.writeInt(getTaken()); parcel.writeInt(getFree()); }

1 个答案:

答案 0 :(得分:0)

您必须按照您写入的相同顺序读取包裹。