如果声明,请不要干杯

时间:2017-08-18 12:40:09

标签: java android xml

当我给小费回答并点击检查应用程序崩溃时 并且没有显示吐司。 我该怎么做才能解决这个问题?    我试着像删除获取文本甚至把它放在if语句中。我该怎么办才能解决这个问题? 但它是一个测验应用程序,正如我在应用程序的基础之前告诉的那样,答案检查器不能正常工作。

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



    public void theFirst() {
        String answer1 = "David Ben Gurion";

        EditText text = (EditText) findViewById(R.id.answer_no_1);
        text.getText().toString();
        if (text.equals(answer1)) {
            Context context = getApplicationContext();
            CharSequence text1 = "Right";
            int duration = Toast.LENGTH_SHORT;

            Toast rightToast = Toast.makeText(context, text1, duration);
            rightToast.show();


        }else {
            Context context = getApplicationContext();
            CharSequence text2 = "Try Again";
            int duration = Toast.LENGTH_SHORT;

            Toast wrongToast = Toast.makeText(context, text2, duration);
            wrongToast.show();

        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.quizapp.MainActivity"
     >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
<TextView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30dp"
    android:layout_gravity="center"
    android:text="The Quiz"
    android:layout_margin="16dp"
    />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Who was the first prime minister of Israel"

            android:layout_marginLeft="17dp"
            />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:hint="Answer"
    android:id="@+id/answer_no_1"

    />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="12dp"
    android:text="Show answer"

    />

            <Button
                android:id="@+id/checkAnswerButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:onClick="theFirst"
                android:text="Check answer" />

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="12dp"

                android:text="hint" />
        </LinearLayout>


    </LinearLayout>
</ScrollView>`

1 个答案:

答案 0 :(得分:0)

试试这个

public void theFirst(View v) {
    String answer1 = "David Ben Gurion";

    EditText text = (EditText) findViewById(R.id.answer_no_1);
 if (text.getText().toString().equals(answer1)) {
        Context context = getApplicationContext();
        CharSequence text1 = "Right";
        int duration = Toast.LENGTH_SHORT;

        Toast rightToast = Toast.makeText(context, text1, duration);
        rightToast.show();


  }else {
        Context context = getApplicationContext();
        CharSequence text2 = "Try Again";
        int duration = Toast.LENGTH_SHORT;

        Toast wrongToast = Toast.makeText(context, text2, duration);
        wrongToast.show();

    }
 }
 }