firebase每次检索数据错误?每一次尝试?

时间:2017-06-11 09:14:43

标签: android firebase firebase-realtime-database

当我尝试使用类包含许多变量写入firebase数据库时,它工作正常但是当我尝试检索数据时,应用程序崩溃! 为什么我会疯了-_-!?

> java代码

 FirebaseDatabase database;
    DatabaseReference myRef;
    TextView text1, text2, text3;
    EditText et1, et2, et3;
    Button bSave, bRead;


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

        database = FirebaseDatabase.getInstance();
        myRef = database.getReference();

        text1 = (TextView) findViewById(R.id.text1);
        text2 = (TextView) findViewById(R.id.text2);
        text3 = (TextView) findViewById(R.id.text3);

        et1 = (EditText) findViewById(R.id.et1);
        et2 = (EditText) findViewById(R.id.et2);
        et3 = (EditText) findViewById(R.id.et3);


        bSave = (Button) findViewById(R.id.bSave);
        bSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1 = et1.getText().toString();
                String str2 = et2.getText().toString();
                String str3 = et3.getText().toString();

                myRef.child("Posts").push().setValue(new text(str1,str2,str3));
            }
        });


        bRead = (Button) findViewById(R.id.bRead);
        bRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myRef=database.getReference().child("Posts");
                myRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        //doesn't work
                        text t1 = dataSnapshot.getValue(text.class);

                        //doesn't work
                        /*for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                            text t1 = snapshot.getValue(text.class);
                        }*/

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.e("MainActivity", "loadPost:onCancelled", databaseError.toException());
                    }
                });
            }
        });


    }

}

> Text.class

public class text {
    String text1,text2,text3;


    public text(String text1, String text2, String text3) {
        this.text1 = text1;
        this.text2 = text2;
        this.text3 = text3;
    }

    public String getText1() {
        return text1;
    }

    public void setText1(String text1) {
        this.text1 = text1;
    }

    public String getText2() {
        return text2;
    }

    public void setText2(String text2) {
        this.text2 = text2;
    }

    public String getText3() {
        return text3;
    }

    public void setText3(String text3) {
        this.text3 = text3;
    }
}

>屏

Screen

1 个答案:

答案 0 :(得分:0)

Firebase需要空构造函数,所以请始终将其保留在此处。

public class text {
    String text1,text2,text3;

    public text() { }

    public text(String text1, String text2, String text3) {
        this.text1 = text1;
        this.text2 = text2;
        this.text3 = text3;
    }