Monday, 15 February 2010

android - Why does setOnCheckedChangeListener? cause crash? -



android - Why does setOnCheckedChangeListener? cause crash? -

for reason app crashes on launch.. heres code , debug output. seems me it's how using setoncheckedchangelistener..

debug output:

02-12 05:12:03.048: e/androidruntime(1464): fatal exception: main 02-12 05:12:03.048: e/androidruntime(1464): java.lang.runtimeexception: unable start activity componentinfo{com.dugley.check.in/com.dugley.check.in.mainactivity}: java.lang.classcastexception: com.dugley.check.in.mainactivity cannot cast android.widget.compoundbutton$oncheckedchangelistener 02-12 05:12:03.048: e/androidruntime(1464): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 02-12 05:12:03.048: e/androidruntime(1464): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 02-12 05:12:03.048: e/androidruntime(1464): @ android.app.activitythread.access$600(activitythread.java:141) 02-12 05:12:03.048: e/androidruntime(1464): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 02-12 05:12:03.048: e/androidruntime(1464): @ android.os.handler.dispatchmessage(handler.java:99) 02-12 05:12:03.048: e/androidruntime(1464): @ android.os.looper.loop(looper.java:137) 02-12 05:12:03.048: e/androidruntime(1464): @ android.app.activitythread.main(activitythread.java:5039) 02-12 05:12:03.048: e/androidruntime(1464): @ java.lang.reflect.method.invokenative(native method) 02-12 05:12:03.048: e/androidruntime(1464): @ java.lang.reflect.method.invoke(method.java:511) 02-12 05:12:03.048: e/androidruntime(1464): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 02-12 05:12:03.048: e/androidruntime(1464): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 02-12 05:12:03.048: e/androidruntime(1464): @ dalvik.system.nativestart.main(native method) 02-12 05:12:03.048: e/androidruntime(1464): caused by: java.lang.classcastexception: com.dugley.check.in.mainactivity cannot cast android.widget.compoundbutton$oncheckedchangelistener 02-12 05:12:03.048: e/androidruntime(1464): @ com.dugley.check.in.mainactivity.oncreate(mainactivity.java:21) 02-12 05:12:03.048: e/androidruntime(1464): @ android.app.activity.performcreate(activity.java:5104) 02-12 05:12:03.048: e/androidruntime(1464): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 02-12 05:12:03.048: e/androidruntime(1464): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144)

main_activity.xml

<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" tools:context=".mainactivity" > <switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:layout_marginbottom="30dp" android:text="service"/> </relativelayout>

mainactivity.java:

package com.dugley.check.in; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.widget.compoundbutton; import android.widget.compoundbutton.oncheckedchangelistener; import android.widget.switch; import android.widget.toast; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); switch s = (switch) findviewbyid(r.id.switch1); if (s != null) { s.setoncheckedchangelistener((oncheckedchangelistener) this); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); homecoming true; } public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { toast.maketext(this, "the switch " + (ischecked ? "on" : "off"), toast.length_short).show(); if(ischecked) { //do stuff when switch on } else { //do stuff when switch if off } } }

you need add together implements oncheckedchangelistener mainactivity

i made code working, see below....

public class mainactivity extends activity implements oncheckedchangelistener{//changed @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); switch s = (switch) findviewbyid(r.id.switch1); if (s != null) { s.setoncheckedchangelistener(this); // changed } }

android uiswitch

No comments:

Post a Comment