android - Trying to get imageview to move back and forth when clicked, no errors showing up in Eclipse, but app crashes -
i've been working on side project , have been hung on part of while. i'm trying separate of code related boat imageview boat class possible. i'm not getting errors , looks right me, app keeps crashing when try run it. here boat class:
package com.cannibal_photographer; import android.content.context; import android.util.attributeset; import android.view.view; import android.widget.imageview; public class boat extends imageview { imageview boatimage = (imageview)findviewbyid(r.id.imageview1); boolean state = true; public boat(context context, attributeset attrs) { super(context, attrs); boatimage.setonclicklistener(new onclicklistener() { @override public void onclick (view v) { if (state) { moveboatforward(-290); } else { moveboatreverse(290); } } }); } public void moveboatforward(int amount){ boatimage.offsettopandbottom(amount); state = !state; } public void moveboatreverse(int amount) { boatimage.offsettopandbottom(290); state = !state; } }
here main activity class:
package com.cannibal_photographer; import android.app.activity; import android.os.bundle; import android.view.menu; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); boat boatobject = new boat(this, null); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
i'm sure there fundamental i'm doing wrong, have no idea is. in right direction appreciated.
edit
here layout.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:background="@drawable/background" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <imagebutton android:id="@+id/imagebutton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:src="@drawable/redsquare" /> <imagebutton android:id="@+id/imagebutton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/imagebutton2" android:layout_torightof="@+id/imagebutton2" android:src="@drawable/redsquare" /> <imagebutton android:id="@+id/imagebutton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/imagebutton1" android:layout_torightof="@+id/imagebutton1" android:src="@drawable/redsquare" /> <imagebutton android:id="@+id/imagebutton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/imagebutton3" android:layout_torightof="@+id/imagebutton3" android:src="@drawable/greensquare" /> <imagebutton android:id="@+id/imagebutton5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/imagebutton4" android:layout_torightof="@+id/imagebutton4" android:src="@drawable/greensquare" /> <imagebutton android:id="@+id/imagebutton6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/imagebutton5" android:layout_torightof="@+id/imagebutton5" android:src="@drawable/greensquare" /> <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/imagebutton4" android:layout_alignright="@+id/imagebutton5" android:layout_marginbottom="102dp" android:layout_marginright="14dp" android:src="@drawable/boat" /> <imageview android:id="@+id/imageview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="invisible" android:layout_alignleft="@+id/imageview1" android:layout_aligntop="@+id/imageview2" android:layout_margintop="135dp" android:src="@drawable/boat" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/imagebutton2" android:layout_aligntop="@+id/imageview2" android:layout_marginright="14dp" android:layout_margintop="16dp" android:text="textview" /> </relativelayout>
here logcat:
10-07 04:01:53.424: d/dalvikvm(2049): gc_for_alloc freed 37k, 6% free 2556k/2712k, paused 26ms, total 29ms 10-07 04:01:53.444: i/dalvikvm-heap(2049): grow heap (frag case) 5.245mb 2764816-byte allocation 10-07 04:01:53.494: d/dalvikvm(2049): gc_for_alloc freed 2k, 3% free 5254k/5416k, paused 41ms, total 41ms 10-07 04:01:53.554: d/dalvikvm(2049): gc_concurrent freed <1k, 3% free 5285k/5448k, paused 3ms+22ms, total 62ms 10-07 04:01:53.604: d/androidruntime(2049): shutting down vm 10-07 04:01:53.604: w/dalvikvm(2049): threadid=1: thread exiting uncaught exception (group=0x40a71930) 10-07 04:01:53.614: e/androidruntime(2049): fatal exception: main 10-07 04:01:53.614: e/androidruntime(2049): java.lang.runtimeexception: unable start activity componentinfo{com.cannibal_photographer/com.cannibal_photographer.mainactivity}: java.lang.nullpointerexception 10-07 04:01:53.614: e/androidruntime(2049): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 10-07 04:01:53.614: e/androidruntime(2049): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 10-07 04:01:53.614: e/androidruntime(2049): @ android.app.activitythread.access$600(activitythread.java:141) 10-07 04:01:53.614: e/androidruntime(2049): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 10-07 04:01:53.614: e/androidruntime(2049): @ android.os.handler.dispatchmessage(handler.java:99) 10-07 04:01:53.614: e/androidruntime(2049): @ android.os.looper.loop(looper.java:137) 10-07 04:01:53.614: e/androidruntime(2049): @ android.app.activitythread.main(activitythread.java:5041) 10-07 04:01:53.614: e/androidruntime(2049): @ java.lang.reflect.method.invokenative(native method) 10-07 04:01:53.614: e/androidruntime(2049): @ java.lang.reflect.method.invoke(method.java:511) 10-07 04:01:53.614: e/androidruntime(2049): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 10-07 04:01:53.614: e/androidruntime(2049): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 10-07 04:01:53.614: e/androidruntime(2049): @ dalvik.system.nativestart.main(native method) 10-07 04:01:53.614: e/androidruntime(2049): caused by: java.lang.nullpointerexception 10-07 04:01:53.614: e/androidruntime(2049): @ com.cannibal_photographer.boat.init(boat.java:22) 10-07 04:01:53.614: e/androidruntime(2049): @ com.cannibal_photographer.boat.<init>(boat.java:18) 10-07 04:01:53.614: e/androidruntime(2049): @ com.cannibal_photographer.mainactivity.oncreate(mainactivity.java:19) 10-07 04:01:53.614: e/androidruntime(2049): @ android.app.activity.performcreate(activity.java:5104) 10-07 04:01:53.614: e/androidruntime(2049): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 10-07 04:01:53.614: e/androidruntime(2049): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 10-07 04:01:53.614: e/androidruntime(2049): ... 11 more 10-07 04:01:56.825: i/process(2049): sending signal. pid: 2049 sig: 9 10-07 04:15:22.295: d/dalvikvm(2136): gc_for_alloc freed 40k, 6% free 2556k/2716k, paused 37ms, total 40ms 10-07 04:15:22.315: i/dalvikvm-heap(2136): grow heap (frag case) 5.245mb 2764816-byte allocation 10-07 04:15:22.365: d/dalvikvm(2136): gc_for_alloc freed 2k, 4% free 5254k/5420k, paused 44ms, total 44ms 10-07 04:15:22.415: d/dalvikvm(2136): gc_concurrent freed <1k, 4% free 5271k/5436k, paused 9ms+4ms, total 59ms 10-07 04:15:22.455: d/androidruntime(2136): shutting down vm 10-07 04:15:22.455: w/dalvikvm(2136): threadid=1: thread exiting uncaught exception (group=0x40a71930) 10-07 04:15:22.475: e/androidruntime(2136): fatal exception: main 10-07 04:15:22.475: e/androidruntime(2136): java.lang.runtimeexception: unable start activity componentinfo{com.cannibal_photographer/com.cannibal_photographer.mainactivity}: android.view.inflateexception: binary xml file line #60: error inflating class com.cannibal_photographer.boat 10-07 04:15:22.475: e/androidruntime(2136): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 10-07 04:15:22.475: e/androidruntime(2136): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 10-07 04:15:22.475: e/androidruntime(2136): @ android.app.activitythread.access$600(activitythread.java:141) 10-07 04:15:22.475: e/androidruntime(2136): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 10-07 04:15:22.475: e/androidruntime(2136): @ android.os.handler.dispatchmessage(handler.java:99) 10-07 04:15:22.475: e/androidruntime(2136): @ android.os.looper.loop(looper.java:137) 10-07 04:15:22.475: e/androidruntime(2136): @ android.app.activitythread.main(activitythread.java:5041) 10-07 04:15:22.475: e/androidruntime(2136): @ java.lang.reflect.method.invokenative(native method) 10-07 04:15:22.475: e/androidruntime(2136): @ java.lang.reflect.method.invoke(method.java:511) 10-07 04:15:22.475: e/androidruntime(2136): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 10-07 04:15:22.475: e/androidruntime(2136): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 10-07 04:15:22.475: e/androidruntime(2136): @ dalvik.system.nativestart.main(native method) 10-07 04:15:22.475: e/androidruntime(2136): caused by: android.view.inflateexception: binary xml file line #60: error inflating class com.cannibal_photographer.boat 10-07 04:15:22.475: e/androidruntime(2136): @ android.view.layoutinflater.createview(layoutinflater.java:596) 10-07 04:15:22.475: e/androidruntime(2136): @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:687) 10-07 04:15:22.475: e/androidruntime(2136): @ android.view.layoutinflater.rinflate(layoutinflater.java:746) 10-07 04:15:22.475: e/androidruntime(2136): @ android.view.layoutinflater.inflate(layoutinflater.java:489) 10-07 04:15:22.475: e/androidruntime(2136): @ android.view.layoutinflater.inflate(layoutinflater.java:396) 10-07 04:15:22.475: e/androidruntime(2136): @ android.view.layoutinflater.inflate(layoutinflater.java:352) 10-07 04:15:22.475: e/androidruntime(2136): @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:270) 10-07 04:15:22.475: e/androidruntime(2136): @ android.app.activity.setcontentview(activity.java:1881) 10-07 04:15:22.475: e/androidruntime(2136): @ com.cannibal_photographer.mainactivity.oncreate(mainactivity.java:16) 10-07 04:15:22.475: e/androidruntime(2136): @ android.app.activity.performcreate(activity.java:5104) 10-07 04:15:22.475: e/androidruntime(2136): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 10-07 04:15:22.475: e/androidruntime(2136): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 10-07 04:15:22.475: e/androidruntime(2136): ... 11 more 10-07 04:15:22.475: e/androidruntime(2136): caused by: java.lang.nosuchmethodexception: <init> [class android.content.context, interface android.util.attributeset] 10-07 04:15:22.475: e/androidruntime(2136): @ java.lang.class.getconstructorormethod(class.java:460) 10-07 04:15:22.475: e/androidruntime(2136): @ java.lang.class.getconstructor(class.java:431) 10-07 04:15:22.475: e/androidruntime(2136): @ android.view.layoutinflater.createview(layoutinflater.java:561) 10-07 04:15:22.475: e/androidruntime(2136): ... 22 more 10-07 04:15:27.784: i/process(2136): sending signal. pid: 2136 sig: 9 10-07 04:21:26.554: d/dalvikvm(2205): gc_for_alloc freed 44k, 7% free 2556k/2720k, paused 29ms, total 31ms 10-07 04:21:26.564: i/dalvikvm-heap(2205): grow heap (frag case) 5.245mb 2764816-byte allocation 10-07 04:21:26.624: d/dalvikvm(2205): gc_for_alloc freed 2k, 4% free 5254k/5424k, paused 51ms, total 51ms 10-07 04:21:26.674: d/dalvikvm(2205): gc_concurrent freed <1k, 4% free 5257k/5424k, paused 3ms+3ms, total 54ms 10-07 04:21:26.714: d/androidruntime(2205): shutting down vm 10-07 04:21:26.714: w/dalvikvm(2205): threadid=1: thread exiting uncaught exception (group=0x40a71930) 10-07 04:21:26.734: e/androidruntime(2205): fatal exception: main 10-07 04:21:26.734: e/androidruntime(2205): java.lang.runtimeexception: unable start activity componentinfo{com.cannibal_photographer/com.cannibal_photographer.mainactivity}: android.view.inflateexception: binary xml file line #60: error inflating class com.cannibal_photographer.boat 10-07 04:21:26.734: e/androidruntime(2205): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 10-07 04:21:26.734: e/androidruntime(2205): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 10-07 04:21:26.734: e/androidruntime(2205): @ android.app.activitythread.access$600(activitythread.java:141) 10-07 04:21:26.734: e/androidruntime(2205): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 10-07 04:21:26.734: e/androidruntime(2205): @ android.os.handler.dispatchmessage(handler.java:99) 10-07 04:21:26.734: e/androidruntime(2205): @ android.os.looper.loop(looper.java:137) 10-07 04:21:26.734: e/androidruntime(2205): @ android.app.activitythread.main(activitythread.java:5041) 10-07 04:21:26.734: e/androidruntime(2205): @ java.lang.reflect.method.invokenative(native method) 10-07 04:21:26.734: e/androidruntime(2205): @ java.lang.reflect.method.invoke(method.java:511) 10-07 04:21:26.734: e/androidruntime(2205): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 10-07 04:21:26.734: e/androidruntime(2205): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 10-07 04:21:26.734: e/androidruntime(2205): @ dalvik.system.nativestart.main(native method) 10-07 04:21:26.734: e/androidruntime(2205): caused by: android.view.inflateexception: binary xml file line #60: error inflating class com.cannibal_photographer.boat 10-07 04:21:26.734: e/androidruntime(2205): @ android.view.layoutinflater.createview(layoutinflater.java:596) 10-07 04:21:26.734: e/androidruntime(2205): @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:687) 10-07 04:21:26.734: e/androidruntime(2205): @ android.view.layoutinflater.rinflate(layoutinflater.java:746) 10-07 04:21:26.734: e/androidruntime(2205): @ android.view.layoutinflater.inflate(layoutinflater.java:489) 10-07 04:21:26.734: e/androidruntime(2205): @ android.view.layoutinflater.inflate(layoutinflater.java:396) 10-07 04:21:26.734: e/androidruntime(2205): @ android.view.layoutinflater.inflate(layoutinflater.java:352) 10-07 04:21:26.734: e/androidruntime(2205): @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:270) 10-07 04:21:26.734: e/androidruntime(2205): @ android.app.activity.setcontentview(activity.java:1881) 10-07 04:21:26.734: e/androidruntime(2205): @ com.cannibal_photographer.mainactivity.oncreate(mainactivity.java:16) 10-07 04:21:26.734: e/androidruntime(2205): @ android.app.activity.performcreate(activity.java:5104) 10-07 04:21:26.734: e/androidruntime(2205): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 10-07 04:21:26.734: e/androidruntime(2205): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 10-07 04:21:26.734: e/androidruntime(2205): ... 11 more 10-07 04:21:26.734: e/androidruntime(2205): caused by: java.lang.nosuchmethodexception: <init> [class android.content.context, interface android.util.attributeset] 10-07 04:21:26.734: e/androidruntime(2205): @ java.lang.class.getconstructorormethod(class.java:460) 10-07 04:21:26.734: e/androidruntime(2205): @ java.lang.class.getconstructor(class.java:431) 10-07 04:21:26.734: e/androidruntime(2205): @ android.view.layoutinflater.createview(layoutinflater.java:561) 10-07 04:21:26.734: e/androidruntime(2205): ... 22 more
here updated version of boat class:
package com.cannibal_photographer; import android.content.context; import android.view.view; import android.widget.imageview; public class boat extends imageview { boolean state = true; public boat(context context) { super(context); init(); } private void init() { this.setonclicklistener(new onclicklistener() { @override public void onclick (view v) { if (state) { moveboatforward(-290); } else { moveboatreverse(290); } } }); } public void moveboatforward(int amount){ this.offsettopandbottom(amount); state = !state; } public void moveboatreverse(int amount) { this.offsettopandbottom(amount); state = !state; } }
a few ideas here:
in boat view, override 3 constructors:
code sample:
public boat(context context) { super(context); init(); } public boat(context context, attributeset attrs) { super(context, attrs); init(); } public boat(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(); } private void init() { boatimage.setonclicklistener(new onclicklistener() { @override public void onclick (view v) { if (state) { moveboatforward(-290); } else { moveboatreverse(290); } } }); } }
i find general practice when extending views.
it best if included boat view in layout.xml file, way inflation process can handle instantating boat view
if must instantate boat via code, use 1st constructor. believe fact sending null attributeset what's causing crash.
unrelated problem, have 2 methods: moveboatforward & moveboatreverse practically same thing. duplicate code , considered bad practice while coding. try think of way merge these 2 methods one, calling same method, once positive number , once negative number.
edit:
here's why crash happening:
imageview boatimage = (imageview)findviewbyid(r.id.imageview1);
you're trying find view before view given context. understand how achieve behavior you're looking for, best if posted layout.xml file.
edit:
considering layout, here's changes should make:
turn imageview boat:
<com.cannibal_photographer.boat android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/imagebutton4" android:layout_alignright="@+id/imagebutton5" android:layout_marginbottom="102dp" android:layout_marginright="14dp" android:src="@drawable/boat" />
in activity, instantate boat this:
boat boatobject = (boat)findviewbyid(r.id.imageview1);
hope helps :)
Comments
Post a Comment