java - One OnClickListener for several buttons in an AlertDialog -
i creating app simulates rolling different types of dice. dice selected via alertdialog full of buttons. want use 1 onclicklistener 6 of these buttons when try set listener, eclipse throws nullpointerexception.
package edu.elon.cs.diceroller; import android.app.activity; import android.app.alertdialog; import android.os.bundle; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.listview; import android.widget.textview; public class rollmenuactivity extends activity { private int diceamount; private int dicesides; private boolean sum; private button adddicebutton; private button rolldicebutton; private listview dicelistview; private button onebutton; private button twobutton; private button threebutton; private button fourbutton; private button fivebutton; private button sixbutton; private button d4; private button d6; private button d8; private button d12; private button d20; private button sumbutton; private button top3button; private button trimbutton; private button avgbutton; private button hilobutton; private button lohibutton; private textview displayquantity; private textview displaytype; private textview displayarrange; private onclicklistener adddicelistener = new onclicklistener() { @override public void onclick(view aarg0) { alertdialog.builder alert = new alertdialog.builder( rollmenuactivity.this); layoutinflater factory = layoutinflater .from(rollmenuactivity.this); view layout = factory.inflate(r.layout.dice_select, null); alert.setview(layout); alert.show(); } }; private onclicklistener rolldicelistener = new onclicklistener() { @override public void onclick(view av) { // todo auto-generated method stub } }; @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_roll_menu, menu); return true; } private onclicklistener quantitylistener = new onclicklistener(){ @override public void onclick(view v) { button abutton = (button) v; string buttonnumber = abutton.gettext().tostring(); diceamount = integer.parseint(buttonnumber.trim()); displayquantity.settext(buttonnumber); } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_roll_menu); adddicebutton = (button) findviewbyid(r.id.adddicebutton); rolldicebutton = (button) findviewbyid(r.id.rolldicebutton); dicelistview = (listview) findviewbyid(r.id.dicelistview); adddicebutton.setonclicklistener(adddicelistener); rolldicebutton.setonclicklistener(rolldicelistener); onebutton = (button) findviewbyid(r.id.button1); twobutton = (button) findviewbyid(r.id.button2); threebutton = (button) findviewbyid(r.id.button3); fourbutton = (button) findviewbyid(r.id.button4); fivebutton = (button) findviewbyid(r.id.button5); sixbutton = (button) findviewbyid(r.id.button6); onebutton.setonclicklistener(quantitylistener); twobutton.setonclicklistener(quantitylistener); threebutton.setonclicklistener(quantitylistener); fourbutton.setonclicklistener(quantitylistener); fivebutton.setonclicklistener(quantitylistener); sixbutton.setonclicklistener(quantitylistener); displayquantity = (textview) findviewbyid(r.id.displayquantity); } } <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=".rollmenuactivity" > <listview android:id="@+id/dicelistview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/adddicebutton" > </listview> <button android:id="@+id/rolldicebutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:text="press or shake roll dice" tools:ignore="hardcodedtext" /> <button android:id="@+id/adddicebutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/rolldicebutton" android:layout_alignparentleft="true" android:text="add dice" tools:ignore="hardcodedtext" /> </relativelayout> <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/diceselectlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <linearlayout android:id="@+id/linearlayout2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_margintop="34dp" > <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" /> <button android:id="@+id/buttond4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="d4" /> <button android:id="@+id/buttonsum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" sum " /> </linearlayout> <linearlayout android:id="@+id/linearlayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/linearlayout5" android:layout_centerhorizontal="true" > <button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5" /> <button android:id="@+id/buttond20" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="d20" /> <button android:id="@+id/buttonhilo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hi-lo" /> </linearlayout> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:text="type" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/linearlayout2" android:layout_alignparenttop="true" android:text="quantity" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_alignright="@+id/linearlayout2" android:text="arrange" android:textappearance="?android:attr/textappearancemedium" /> <linearlayout android:id="@+id/linearlayout3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/linearlayout2" android:layout_below="@+id/linearlayout2" > <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" /> <button android:id="@+id/buttond6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="d6" /> <button android:id="@+id/buttontop3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="top 3" /> </linearlayout> <linearlayout android:id="@+id/linearlayout4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/linearlayout3" android:layout_below="@+id/linearlayout3" > <button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" /> <button android:id="@+id/buttond8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="d8" /> <button android:id="@+id/buttontrim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="trim " /> </linearlayout> <linearlayout android:id="@+id/linearlayout5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/linearlayout4" android:layout_below="@+id/linearlayout4" > <button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" /> <button android:id="@+id/buttond12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="d12" /> <button android:id="@+id/buttonavg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" avg. " /> </linearlayout> <linearlayout android:id="@+id/linearlayout6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/linearlayout1" android:layout_centerhorizontal="true" > <button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6" /> <button android:id="@+id/button17" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " /> <button android:id="@+id/buttonlohi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="lo-hi" /> </linearlayout> <textview android:id="@+id/displayquantity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/linearlayout5" android:layout_below="@+id/linearlayout6" android:layout_margintop="17dp" android:text=" " android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/displaytype" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/textview4" android:layout_centerhorizontal="true" android:text=" " android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/displayarrange" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textview5" android:layout_alignbottom="@+id/textview5" android:layout_alignright="@+id/linearlayout2" android:text=" " android:textappearance="?android:attr/textappearancemedium" /> </relativelayout>
instead of creating variables hold instances of onclicklistener
implementation, use classes implement onclicklistener
, override onclick
method.
during oncreate
assign new instances of objects buttons
it's better practice when working android sdk instantiate objects during oncreate
rather relying on class instantiate them during creation.
edit: fixed integer.parseinteger
method had. throw exception if can't parse it.
// create classes implement onclicklistener private class onadddicelistener implements onclicklistener { public void onclick(view v) { layoutinflater inflater = layoutinflater.from(v.getcontext()); view layout = inflater.inflate(r.layout.dice_select, null); builder builder = new builder(v.getcontext()); builder.setview(layout); alertdialog alert = builder.create(); alert.show(); } } private class onrolldicelistener implements onclicklistener { public void onclick(view v) { } } private class onquantitylistener implements onclicklistener { public void onclick(view v) { button button = (button) v; try { diceamount = integer.parseint(button.gettext().tostring()); } catch(numberformatexception e) { diceamount = 0; e.printstacktrace(); } displayquantity.settext(diceamount); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_roll_menu, menu); return true; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_roll_menu); adddicebutton = (button) findviewbyid(r.id.adddicebutton); rolldicebutton = (button) findviewbyid(r.id.rolldicebutton); dicelistview = (listview) findviewbyid(r.id.dicelistview); // since oncreate method, instantiate listeners buttons adddicebutton.setonclicklistener(new onadddicelistener()); rolldicebutton.setonclicklistener(new onrolldicelistener()); onebutton = (button) findviewbyid(r.id.button1); twobutton = (button) findviewbyid(r.id.button2); threebutton = (button) findviewbyid(r.id.button3); fourbutton = (button) findviewbyid(r.id.button4); fivebutton = (button) findviewbyid(r.id.button5); sixbutton = (button) findviewbyid(r.id.button6); // instantiate onquantitylistener onebutton.setonclicklistener(new onquantitylistener()); twobutton.setonclicklistener(new onquantitylistener()); threebutton.setonclicklistener(new onquantitylistener()); fourbutton.setonclicklistener(new onquantitylistener()); fivebutton.setonclicklistener(new onquantitylistener()); sixbutton.setonclicklistener(new onquantitylistener()); displayquantity = (textview) findviewbyid(r.id.displayquantity); }
Comments
Post a Comment