从Firebase数据库检索数据时出错

时间:2019-08-17 10:34:41

标签: java android firebase-realtime-database

我的应用始终在启动时崩溃。我的代码基于从Firebase实时数据库中检索数据并将值放在textviews中。然后单击按钮,它将比较用户输入的答案和真实答案,并检查它们是否匹配。也许我的代码被android studio放置的finales破坏了,因为如果我不输入finals将会是错误的。也许错误在于从firebase中检索数据,因为这是我第一次在android studio中这样做。

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    TextView questionText;
    TextInputEditText answerText;
    TextView hintText;
    Button button;
    FirebaseDatabase database;
    DatabaseReference myRef;
    String question;
    String hint;
    String answer;

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

        questionText = findViewById(R.id.question);
        hintText = findViewById(R.id.hint);
        answerText = findViewById(R.id.input_answer);
        button = findViewById(R.id.button);
        final setClass setClass = new setClass();
        final int cnt = 1;

        database = FirebaseDatabase.getInstance();

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.

                for (DataSnapshot dataSnapshot0 : dataSnapshot.getChildren()) {

                    dataSnapshot0.getValue(com.example.quizremastered.setClass.class);

                    setClass.SetQuestion(dataSnapshot0.child(String.valueOf(cnt)).getValue(com.example.quizremastered.setClass.class).GetQuestion());
                    setClass.SetHint(dataSnapshot0.child(String.valueOf(cnt)).getValue(com.example.quizremastered.setClass.class).GetHint());
                    setClass.SetAnswer(dataSnapshot0.child(String.valueOf(cnt)).getValue(com.example.quizremastered.setClass.class).GetAnswer());

                }
            }


            @Override
            public void onCancelled(DatabaseError error) {
            }


        });

        questionText.setText(setClass.GetQuestion());
        hintText.setText(setClass.GetHint());

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String answerInput = answerText.getEditableText().toString().trim();
                if (answerInput.isEmpty()) {
                    answerText.setError("Field can't be empty");
                } else {
                    answerText.setError(null);
                    if (answerInput == setClass.GetAnswer()) {
                        button.setBackgroundColor(GREEN);
                    }
                    //else
                    {
                        button.setBackgroundColor(GREEN);
                    }
                }
            }
        });

setClass.java:

package com.example.quizremastered;

public class setClass {
    private String question;
    private String hint;
    private String answer;

    public setClass(){
    }

    public String GetQuestion() {
        return question;
    }

    public String GetHint() {
        return hint;
    }

    public String GetAnswer() {
        return answer;
    }

    public void SetQuestion(String question) {
        this.question = question;
    }

    public void SetHint(String hint) {
        this.hint = hint;
    }

    public void SetAnswer(String answer) {
        this.answer = answer;
    }


}

activity_main.xml:

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="60dp">

        <TextView
            android:id="@+id/question"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Question:"
            android:layout_marginTop="100sp"
            android:textSize="30sp"
            />

        <TextView
            android:id="@+id/hint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10sp"
            android:text="Hint:"
            android:layout_below="@id/question"
            android:textSize="15sp"
            />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/input_answer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/hint"
            app:counterEnabled="true"
            app:counterMaxLength="20"
            app:errorEnabled="true">


            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Answer"
                android:layout_marginTop="20sp"
                android:inputType="text"
                android:maxLength="20"
                android:textSize="20sp" />
        </com.google.android.material.textfield.TextInputLayout>

<Button
    android:id="@+id/button"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="Answer"
    android:layout_below="@id/input_answer"
/>

0 个答案:

没有答案
相关问题