android - setOnCheckedListener will crash my app -
i trying set toggle-button. created button in xml-file, when activate setoncheckedchangelistener(this); app crashs.
if comment out, there no problem run app.
layout:
<?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:orientation="vertical" android:id="@+id/layout"> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="crunch" android:textappearance="?android:attr/textappearancelarge" /> <scrollview android:id="@+id/scrollview1" android:layout_width="wrap_content" android:layout_height="match_parent" > <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <horizontalscrollview android:id="@+id/horizontalscrollview1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <imageview android:id="@+id/imageview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </linearlayout> </horizontalscrollview> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/crunch" /> <textview android:id="@+id/tvtimer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> <togglebutton android:id="@+id/togglebutton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textoff="pause" android:texton="play" /> </linearlayout> </scrollview> </linearlayout>
activity:
public class oberklasse extends activity implements oninitlistener, oncheckedchangelistener { public texttospeech tts; togglebutton t; linearlayout l; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); tts = new texttospeech(this, this); //start und stop button t=(togglebutton) findviewbyid(r.id.togglebutton1); t.setoncheckedchangelistener(this); l=(linearlayout)findviewbyid(r.id.layout); } @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { // todo auto-generated method stub if (ischecked) { l.setbackgroundcolor(color.blue); } else { l.setbackgroundcolor(color.black); } }
error-code:
03-24 23:23:52.544: e/androidruntime(1787): fatal exception: main 03-24 23:23:52.544: e/androidruntime(1787): java.lang.runtimeexception: unable start activity componentinfo{com.example.bikinifigur2/de.basti12354.tage.uebungen.tag1}: java.lang.nullpointerexception 03-24 23:23:52.544: e/androidruntime(1787): @ android.app.activitythread.performlaunchactivity(activitythread.java:2211) 03-24 23:23:52.544: e/androidruntime(1787): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2261) 03-24 23:23:52.544: e/androidruntime(1787): @ android.app.activitythread.access$600(activitythread.java:141) 03-24 23:23:52.544: e/androidruntime(1787): @ android.app.activitythread$h.handlemessage(activitythread.java:1256) 03-24 23:23:52.544: e/androidruntime(1787): @ android.os.handler.dispatchmessage(handler.java:99) 03-24 23:23:52.544: e/androidruntime(1787): @ android.os.looper.loop(looper.java:137) 03-24 23:23:52.544: e/androidruntime(1787): @ android.app.activitythread.main(activitythread.java:5103) 03-24 23:23:52.544: e/androidruntime(1787): @ java.lang.reflect.method.invokenative(native method) 03-24 23:23:52.544: e/androidruntime(1787): @ java.lang.reflect.method.invoke(method.java:525) 03-24 23:23:52.544: e/androidruntime(1787): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 03-24 23:23:52.544: e/androidruntime(1787): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 03-24 23:23:52.544: e/androidruntime(1787): @ dalvik.system.nativestart.main(native method) 03-24 23:23:52.544: e/androidruntime(1787): caused by: java.lang.nullpointerexception 03-24 23:23:52.544: e/androidruntime(1787): @ de.basti12354.tage.uebungen.oberklasse.oncreate(oberklasse.java:36) 03-24 23:23:52.544: e/androidruntime(1787): @ de.basti12354.tage.uebungen.tag1.oncreate(tag1.java:45) 03-24 23:23:52.544: e/androidruntime(1787): @ android.app.activity.performcreate(activity.java:5133) 03-24 23:23:52.544: e/androidruntime(1787): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 03-24 23:23:52.544: e/androidruntime(1787): @ android.app.activitythread.performlaunchactivity(activitythread.java:2175) 03-24 23:23:52.544: e/androidruntime(1787): ... 11 more
the problem haven't called setcontentview(r.layout.<layout-file-name>)
before finding , storing references view.
Comments
Post a Comment