java - Android setImageResource causing application to crash -
to start off i'm pretty new android programming error may extremely simple didn't notice. whenever open simple app on starts image. when swipe it's supposed change different image instead app crashes. ideas why is?
here java code
package example.course.bananapeeler; import android.gesture.gesture; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.view; import android.view.menuitem; import android.widget.button; import android.widget.textview; import android.widget.imageview; import android.view.motionevent; import android.view.gesturedetector; import android.support.v4.view.gesturedetectorcompat; public class mainactivity extends actionbaractivity implements gesturedetector.ongesturelistener, gesturedetector.ondoubletaplistener{ private imageview mainpic; private gesturedetectorcompat gesturedetector; private int moneypersecond; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); this.gesturedetector = new gesturedetectorcompat(this, this); gesturedetector.setondoubletaplistener(this); //imageview mainpic = (imageview)findviewbyid(r.id.mainpic); imageview mainpic = (imageview)findviewbyid(r.id.mainpic); } @override public boolean onsingletapconfirmed(motionevent motionevent) { return false; } @override public boolean ondoubletap(motionevent motionevent) { return false; } @override public boolean ondoubletapevent(motionevent motionevent) { return false; } @override public boolean ondown(motionevent motionevent) { return true; } @override public void onshowpress(motionevent motionevent) { } @override public boolean onsingletapup(motionevent motionevent) { return false; } @override public boolean onscroll(motionevent motionevent, motionevent motionevent2, float v, float v2) { return false; } @override public void onlongpress(motionevent motionevent) { } @override public boolean onfling(motionevent motionevent, motionevent motionevent2, float v, float v2) { /* imageview img = new imageview(this); findviewbyid(r.id.mainpic); */ mainpic.setimageresource(r.drawable.peeledbanana); return true; } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean ontouchevent(motionevent event) { this.gesturedetector.ontouchevent(event); return super.ontouchevent(event); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
the xml file:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"><![cdata[ /"peeledbanana"; android:id="@+id/imageview" android:layout_centervertical="true" android:layout_centerhorizontal="true" /> ]]> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/mainpic" android:layout_centervertical="true" android:layout_centerhorizontal="true" android:src="@drawable/mainbanana" android:contentdescription="@string/mainpic" /> </relativelayout>
not sure why format of syntax file strange. causing crashes?
logcat errors
05-13 17:16:47.329 2333-2333/example.course.bananapeeler e/inputeventreceiver﹕ exception dispatching input event. 05-13 17:16:47.329 2333-2333/example.course.bananapeeler e/messagequeue-jni﹕ exception in messagequeue callback: handlereceivecallback 05-13 17:16:47.339
2333-2333/example.course.bananapeeler e/messagequeue-jni﹕ java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.imageview.setimageresource(int)' on null object reference @ example.course.bananapeeler.mainactivity.onfling(mainactivity.java:88) @ android.view.gesturedetector.ontouchevent(gesturedetector.java:620) @ android.support.v4.view.gesturedetectorcompat$gesturedetectorcompatimpljellybeanmr2.ontouchevent(gesturedetectorcompat.java:475) @ android.support.v4.view.gesturedetectorcompat.ontouchevent(gesturedetectorcompat.java:538) @ example.course.bananapeeler.mainactivity.ontouchevent(mainactivity.java:102) @ android.app.activity.dispatchtouchevent(activity.java:2869) @ android.support.v7.internal.view.windowcallbackwrapper.dispatchtouchevent(windowcallbackwrapper.java:59) @ com.android.internal.policy.impl.phonewindow$decorview.dispatchtouchevent(phonewindow.java:2529) @ android.view.view.dispatchpointerevent(view.java:9180) @ android.view.viewrootimpl$viewpostimeinputstage.processpointerevent(viewrootimpl.java:4734) @ android.view.viewrootimpl$viewpostimeinputstage.onprocess(viewrootimpl.java:4572) @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:4096) @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:4149) @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:4115) @ android.view.viewrootimpl$asyncinputstage.forward(viewrootimpl.java:4229) @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:4123) @ android.view.viewrootimpl$asyncinputstage.apply(viewrootimpl.java:4286) @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:4096) @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:4149) @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:4115) @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:4123) @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:4096) @ android.view.viewrootimpl.deliverinputevent(viewrootimpl.java:6600) @ android.view.viewrootimpl.doprocessinputevents(viewrootimpl.java:6484) @ android.view.viewrootimpl.enqueueinputevent(viewrootimpl.java:6455) @ android.view.viewrootimpl$windowinputeventreceiver.oninputevent(viewrootimpl.java:6690) @ android.view.inputeventreceiver.dispatchinputevent(inputeventreceiver.java:185) @ android.os.messagequeue.nativepollonce(native method) @ android.os.messagequeue.next(messagequeue.java:143) @ android.os.looper.loop(looper.java:130) @ android.app.activitythread.main(activitythread.java:5972) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1388) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1183) 05-13 17:16:47.339 2333-2333/example.course.bananapeeler d/androidruntime﹕ shutting down vm 05-13 17:16:47.339
2333-2333/example.course.bananapeeler e/androidruntime﹕ fatal exception: main process: example.course.bananapeeler, pid: 2333 java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.imageview.setimageresource(int)' on null object reference @ example.course.bananapeeler.mainactivity.onfling(mainactivity.java:88) @ android.view.gesturedetector.ontouchevent(gesturedetector.java:620) @ android.support.v4.view.gesturedetectorcompat$gesturedetectorcompatimpljellybeanmr2.ontouchevent(gesturedetectorcompat.java:475) @ android.support.v4.view.gesturedetectorcompat.ontouchevent(gesturedetectorcompat.java:538) @ example.course.bananapeeler.mainactivity.ontouchevent(mainactivity.java:102) @ android.app.activity.dispatchtouchevent(activity.java:2869) @ android.support.v7.internal.view.windowcallbackwrapper.dispatchtouchevent(windowcallbackwrapper.java:59) @ com.android.internal.policy.impl.phonewindow$decorview.dispatchtouchevent(phonewindow.java:2529) @ android.view.view.dispatchpointerevent(view.java:9180) @ android.view.viewrootimpl$viewpostimeinputstage.processpointerevent(viewrootimpl.java:4734) @ android.view.viewrootimpl$viewpostimeinputstage.onprocess(viewrootimpl.java:4572) @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:4096) @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:4149) @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:4115) @ android.view.viewrootimpl$asyncinputstage.forward(viewrootimpl.java:4229) @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:4123) @ android.view.viewrootimpl$asyncinputstage.apply(viewrootimpl.java:4286) @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:4096) @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:4149) @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:4115) @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:4123) @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:4096) @ android.view.viewrootimpl.deliverinputevent(viewrootimpl.java:6600) @ android.view.viewrootimpl.doprocessinputevents(viewrootimpl.java:6484) @ android.view.viewrootimpl.enqueueinputevent(viewrootimpl.java:6455) @ android.view.viewrootimpl$windowinputeventreceiver.oninputevent(viewrootimpl.java:6690) @ android.view.inputeventreceiver.dispatchinputevent(inputeventreceiver.java:185) @ android.os.messagequeue.nativepollonce(native method) @ android.os.messagequeue.next(messagequeue.java:143) @ android.os.looper.loop(looper.java:130) @ android.app.activitythread.main(activitythread.java:5972) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1388) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1183)
you're initializing mainpic locally instead of class variable.
change
imageview mainpic = (imageview)findviewbyid(r.id.mainpic);
to
mainpic = (imageview)findviewbyid(r.id.mainpic);
Comments
Post a Comment