android - Trying to simply set button color and text upon press. Keeps crashing? -
so here code, trying have text , color of button posneg_btn
change when pressed. keeps crashing , giving me null pointer exception? ideas? missing?
listview recurrencelistview; button add_btn; edittext reccurenceentry_et; edittext reccurenceexplanation_et; button clear_btn; button posneg_btn; boolean positiveiftrue = false; //double[] comparisonarray = new double[8]; int reccurencecounter; string[] reccurencearray = new string[30]; string[] reccurenceexparray = new string[30]; string[] finaldisplayarray = new string[30]; private arrayadapter arrayadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.forecast); recurrencelistview = (listview) findviewbyid( r.id.recurrencelistview ); reccurenceentry_et = (edittext) findviewbyid( r.id.reccurenceentry_et ); reccurenceexplanation_et = (edittext) findviewbyid( r.id.reccurenceexplanation_et ); button add_btn = (button) findviewbyid( r.id.add_btn); button clear_btn = (button) findviewbyid( r.id.clear_btn); button posneg_btn = (button) findviewbyid( r.id.posneg_btn); add_btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub addrecurrence(); //calculatemovingaverage(); } }); posneg_btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub changesign(); //calculatemovingaverage(); } }); clear_btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub clearreccurences(); //calculatemovingaverage(); } }); } public void changesign(){ posneg_btn.settext("test"); posneg_btn.settextcolor(color.green); /* if(positiveiftrue ==false){ positiveiftrue = true; posneg_btn.settext("+"); // posneg_btn.setbackgroundcolor(color.green); } if(positiveiftrue == true){ positiveiftrue = false; posneg_btn.settext("-"); // posneg_btn.setbackgroundcolor(color.red); } }
forecast.xml
<?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" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="specific bills/recurrences" /> <edittext android:id="@+id/reccurenceentry_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="specific bills/recurrences" android:inputtype="numberdecimal" > <requestfocus /> </edittext> <edittext android:id="@+id/reccurenceexplanation_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="explanation" /> <listview android:id="@+id/recurrencelistview" android:layout_width="114dp" android:layout_height="119dp" > </listview> <spinner android:id="@+id/spinner1" android:layout_width="191dp" android:layout_height="48dp" /> <button android:id="@+id/add_btn" android:layout_width="77dp" android:layout_height="wrap_content" android:text="add" /> <button android:id="@+id/clear_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="clear reccurences" /> <button android:id="@+id/posneg_btn" android:layout_width="60dp" android:layout_height="60dp" android:text="-" android:textsize="40sp" />
logcat
09-10 09:46:06.935: e/androidruntime(12964): fatal exception: main 09-10 09:46:06.935: e/androidruntime(12964): process: com.example.forecastspending, pid: 12964 09-10 09:46:06.935: e/androidruntime(12964): java.lang.nullpointerexception 09-10 09:46:06.935: e/androidruntime(12964): @ com.example.forecastspending.savingsforecastactivity.changesign(savingsforecastactivity.java:102) 09-10 09:46:06.935: e/androidruntime(12964): @ com.example.forecastspending.savingsforecastactivity$2.onclick(savingsforecastactivity.java:79) 09-10 09:46:06.935: e/androidruntime(12964): @ android.view.view.performclick(view.java:4442) 09-10 09:46:06.935: e/androidruntime(12964): @ android.view.view$performclick.run(view.java:18473) 09-10 09:46:06.935: e/androidruntime(12964): @ android.os.handler.handlecallback(handler.java:733) 09-10 09:46:06.935: e/androidruntime(12964): @ android.os.handler.dispatchmessage(handler.java:95) 09-10 09:46:06.935: e/androidruntime(12964): @ android.os.looper.loop(looper.java:136) 09-10 09:46:06.935: e/androidruntime(12964): @ android.app.activitythread.main(activitythread.java:5105) 09-10 09:46:06.935: e/androidruntime(12964): @ java.lang.reflect.method.invokenative(native method) 09-10 09:46:06.935: e/androidruntime(12964): @ java.lang.reflect.method.invoke(method.java:515) 09-10 09:46:06.935: e/androidruntime(12964): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:792) 09-10 09:46:06.935: e/androidruntime(12964): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:608) 09-10 09:46:06.935: e/androidruntime(12964): @ dalvik.system.nativestart.main(native method)
on code:
button posneg_btn = (button) findviewbyid( r.id.posneg_btn);
remove button. must this:
posneg_btn = (button) findviewbyid( r.id.posneg_btn);
same other buttons
the problem you're creating local variable. way you're setting listener variable. when function you're using field, null, because set local variable , not field.
Comments
Post a Comment