android - Simple intent crashes app -
new android.
i'm trying create simple intent in onclicklistener open new page. however, app crashing when click button. i'm not sure wrong.
here's listener
buttoncheat.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent cheatview = new intent(getapplicationcontext(),cheatactivity.class); startactivity(cheatview); }
here's have declared activities in manifest file
<application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".quizactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".cheatactivity" android:label="@string/app_name" /> </application>
here relevant logs
10-03 01:51:23.770 2630-2630/? d/cheatactivity﹕ oncreate() called 10-03 01:51:23.777 2630-2630/? d/androidruntime﹕ shutting down vm 10-03 01:51:23.777 2630-2630/? e/androidruntime﹕ fatal exception: main process: itp341.exercises.week6.geoquiz, pid: 2630 java.lang.runtimeexception: unable start activity componentinfo{itp341.exercises.week6.geoquiz/itp341.exercises.week6.geoquiz.cheatactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string android.content.componentname.tostring()' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2325) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2387) @ android.app.activitythread.access$800(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1303) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string android.content.componentname.tostring()' on null object reference @ itp341.exercises.week6.geoquiz.cheatactivity.oncreate(cheatactivity.java:49) @ android.app.activity.performcreate(activity.java:5990) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1106) @ android.app.activitythread.performlaunchactivity(activitythread.java:2278) at android.app.activitythread.handlelaunchactivity(activitythread.java:2387) at android.app.activitythread.access$800(activitythread.java:151) at android.app.activitythread$h.handlemessage(activitythread.java:1303) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:135) at android.app.activitythread.main(activitythread.java:5254) at java.lang.reflect.method.invoke(native method) at java.lang.reflect.method.invoke(method.java:372) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) at com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
from cheatactivity
public class cheatactivity extends activity { private static final string tag = "cheatactivity"; // view references textview textanswer; button buttonshowanswer; // instance variables boolean answeristrue; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.d(tag, "oncreate() called"); setcontentview(r.layout.activity_cheat); log.d(tag, getcallingactivity().tostring()); = //find views textanswer = (textview) findviewbyid(r.id.text_answer); buttonshowanswer = (button) findviewbyid(r.id.button_show_answer); } }
intent cheatview = new intent(getapplicationcontext(),cheatactivity.class);
this why exception existed,try use replace it,
intent cheatview = new intent(quizactivity.this,cheatactivity.class);
beacause application context can't start activity.
Comments
Post a Comment