I'm getting a NullPointerException when I click a button on my Android app -
i’m working on simple quiz app user presented question , can select either true or false answer question. added “show answer” button, when click on button i’m getting following nullpointerexception. think i'm missing in activity_cheat.xml can't figure out what.
05-31 19:30:59.238 5978-5978/com.bignerdranch.android.geoquiz.geoquiz e/androidruntime﹕ fatal exception: main process: com.bignerdranch.android.geoquiz.geoquiz, pid: 5978 java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(int)' on null object reference @ com.bignerdranch.android.geoquiz.geoquiz.cheatactivity$1.onclick(cheatactivity.java:33) @ android.view.view.performclick(view.java:5197) @ android.view.view$performclick.run(view.java:20926) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:145) @ android.app.activitythread.main(activitythread.java:5942) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1400) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1195)
cheatactivity.java
package com.bignerdranch.android.geoquiz.geoquiz; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.textview; public class cheatactivity extends activity { private boolean mansweristrue; private textview manswertextview; private button mshowanswer; public static final string extra_answer_is_true = "com.bignerdranch.android.geoquiz.answer_is_true"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_cheat); mansweristrue = getintent().getbooleanextra(extra_answer_is_true, false); mshowanswer = (button)findviewbyid(r.id.showanswerbutton); mshowanswer.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (mansweristrue) { manswertextview.settext(r.string.true_button); } else { manswertextview.settext(r.string.false_button); } } }); } }
activity_cheat.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> android:gravity="center"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp" android:text="@string/warning_text_view" /> <textview android:id="@+id/answertextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp" /> <button android:id="@+id/showanswerbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/show_answer_button" /> </linearlayout>
manswertextview
never initialized. that's why npe.
add
manswertextview = (textview)findviewbyid(r.id.answertextview);
before
if (mansweristrue)
Comments
Post a Comment