0

When I tipping the answer and hit check the app crashes and the toast in not shown. What dhould I do to fix this problem??? I tryed evreyhing like deleting the get text an even putting it in the if statment. What should I do to fix it??? Butaway its a quiz app and as I told before the base of the app wich is the answer checker is not working propatly.

    @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>`
Tal
  • 21
  • 7
  • Your `onClick` method needs a `View` parameter - `public void theFirst(View v)`. – Mike M. Aug 18 '17 at 12:43
  • And you are comparing an EditText and a String `EditText text = (EditText) findViewById(R.id.answer_no_1); text.getText().toString(); if (text.equals(answer1)) { [...]` Furthermore you haven't attached the crash log. – Ch4t4r Aug 18 '17 at 12:44
  • First change public void theFirst() to public void theFirst(View view) and in if condition insted of if(text.equals(answer1) set if(text.getText().toString().equals(answer1) – Shailesh Bandil Aug 18 '17 at 12:47

1 Answers1

0

try this

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();

    }
 }
 }
AskNilesh
  • 63,753
  • 16
  • 113
  • 150