java - Not working toast in if statment -
this question has answer here:
when tipping answer , hit check app crashes , toast in not shown. dhould fix problem??? tryed evreyhing deleting text putting in if statment. should fix it??? butaway quiz app , told before base of app wich answer checker 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 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>`
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(); } } }
Comments
Post a Comment