I'm making a fragment of language game in which will be a matching of the word and the translation. I need to store the previously clicked button and after clicking on another button compare them, and if they match to make them invisible.
But I'm getting an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.Button.getText()' on a null object reference
On the line String text = buf.getText().toString();
All buttons were defined before.
#Override
public void onClick(View v) {
if (button_prev != 9) {
if (v.getId() != button_prev) {
Button buf = (Button) v.findViewById(button_prev);
String text = buf.getText().toString();
if (book_array.indexOf(text) - book_array.indexOf((String) ((Button) v).getText()) != 0) {
// book_array obj contains word and translating
i--;
v.findViewById(v.getId()).setVisibility(View.INVISIBLE);
v.findViewById(button_prev).setVisibility(View.INVISIBLE);
}
}
}
switch (v.getId()) {
// Storage the previously clicked the button in button_prev
case R.id.button1:
button_prev = Button_1.getId();
// button_prev = R.id.button1;
break;
case R.id.button2:
button_prev = Button_2.getId();
break;
case R.id.button3:
button_prev = Button_3.getId();
break;
case R.id.button4:
button_prev = Button_4.getId();
break;
case R.id.button5:
button_prev = Button_5.getId();
break;
case R.id.button6:
button_prev = Button_6.getId();
break;
case R.id.button7:
button_prev = Button_7.getId();
break;
case R.id.button8:
button_prev = Button_8.getId();
break;
default:
break;
Maybe someone could fix the code? Is there any correct way to store clicked buttons ?
You can declare a POJO for lastclicked view like following to store lastClicked view.
public class LastViewClicked {
private View view;
public View getView() {
return view;
}
public void setView(View view) {
this.view = view;
}
}
Now to when you click on any button, update the view Object inside LastClickedView using setView Method and when you need to know what was the last clicked button, you create a switch statement with all Ids as cases and if it matches you can do your operation.
// This Function gets the last clicked item which has opened up the Alert Dialog for
// selection and Updates its UI...
private void UpdateLastClickedView(int position, ArrayAdapter arrayAdapter) {
View v = lastViewClicked.getView();
// cast according to your views...
AppCompatTextView appCompatTextView = (AppCompatTextView) v;
// do your operation...
switch(appCompatTextView.getId()){
case R.id.tv1:
// hide or whatever you want to do...
break;
case R.id.tv2:
// hide or whatever you want to do...
break;
}
}
Your null pointer error may be the result of fragment recreation, which results in the value of button_prev invalid. And, the initial value of button_prev may be the reason.
findViewById is a time consuming function. The view binding should be processed once, and once only, in onCreate()
It's better if using MVC, MVP or MVVM. User a model to describe your game logic. Focus on the data change, and the UI should be changed with it.
Declare button_prev as field in your activity or fragment and use getter and setter to access it inside onclick.
Related
i recently integrated butterknife in my android project, and now i am trying to use #OnCheckedChanged annotation for radiogroup. but getting error of not giving callback. So what is the right method to call and get checkedId or this one is for radiobutton only and not for radiogroup.
#OnCheckedChanged(R.id.gendergroupid)
void onGenderSelected(RadioGroup group, int checkedId){
switch(checkedId){
case R.id.maleid:
maleid.setEnabled(true);
maleid.setChecked(true);
break;
case R.id.femaleid:
femaleid.setEnabled(true);
femaleid.setChecked(true);
break;
case R.id.bothid:
bothid.setEnabled(true);
bothid.setChecked(true);
break;
}
}
Gives me error
BloError:(89, 10) error: Unable to match #OnCheckedChanged method arguments.
Parameter #1: android.widget.RadioGroup
did not match any listener parameters
Parameter #2: int
did not match any listener parameters
Methods may have up to 2 parameter(s):
android.widget.CompoundButton
boolean
These may be listed in any order but will be searched for from top to bottom.ckquote
According the specification, this annotation needs to be used with 2 parameters, a CompoundButton and a boolean, so if you really want to use this listener, you have to change it like this:
#OnCheckedChanged(R.id.gendergroupid)
void onGenderSelected(CompoundButton button, boolean checked) {
//do your stuff.
}
I think in your case this listener doesn't work, so you can use another implementation like:
#OnClick({R.id.radio_1, R.id.radio_2})
public void onRadioButtonClicked(RadioButton radioButton) {
// Is the button now checked?
boolean checked = radioButton.isChecked();
// Check which radio button was clicked
switch (radioButton.getId()) {
case R.id.radio_1:
if (checked) {
// 1 clicked
}
break;
case R.id.radio_2:
if (checked) {
// 2 clicked
}
break;
}
}
this worked for me
#OnCheckedChanged({R.id.radio_button1, R.id.radio_button2})
public void onRadioButtonCheckChanged(CompoundButton button, boolean checked) {
if(checked) {
switch (button.getId()) {
case R.id.radio_button1:
// do stuff
break;
case R.id.radio_button2:
// do stuff
break;
}
}
}
I have 50 Buttons. I use the switch statement as shown below. Each case has the same On-Click code. But I make many changes to the code for each Button, and it's really tedious work and very repetitive to change the code the same way 50 times. My code currently looks like this:
public void ButtonOnClick(View v) {
switch (v.getId()) {
case button1:
if(button_list.get(1).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(1).getText().toString());
is_clicked= (String)button_list.get(1).getTag();
if ("0".equals(is_clicked)){
button_list.get(1).setBackgroundResource(R.drawable.button_pressed);
button_list.get(1).setTag("1");
}else{
button_list.get(1).setBackgroundResource(R.drawable.button_normal);
button_list.get(1).setTag("0");
}
break;
case button2:
if(button_list.get(2).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(2).getText().toString());
is_clicked= (String)button_list.get(2).getTag();
if ("0".equals(is_clicked)){
button_list.get(2).setBackgroundResource(R.drawable.button_pressed);
button_list.get(2).setTag("1");
}else{
button_list.get(2).setBackgroundResource(R.drawable.button_normal);
button_list.get(2).setTag("0");
}
break;
case button3:
if(button_list.get(3).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(3).getText().toString());
is_clicked= (String)button_list.get(3).getTag();
if ("0".equals(is_clicked)){
button_list.get(3).setBackgroundResource(R.drawable.button_pressed);
button_list.get(3).setTag("1");
}else{
button_list.get(3).setBackgroundResource(R.drawable.button_normal);
button_list.get(3).setTag("0");
}
break;
case button4:
if(button_list.get(4).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(4).getText().toString());
is_clicked= (String)button_list.get(4).getTag();
if ("0".equals(is_clicked)){
button_list.get(4).setBackgroundResource(R.drawable.button_pressed);
button_list.get(4).setTag("1");
}else{
button_list.get(4).setBackgroundResource(R.drawable.button_normal);
button_list.get(4).setTag("0");
}
break;
case button5:
if(button_list.get(5).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(5).getText().toString());
is_clicked= (String)button_list.get(5).getTag();
if ("0".equals(is_clicked)){
button_list.get(5).setBackgroundResource(R.drawable.button_pressed);
button_list.get(5).setTag("1");
}else{
button_list.get(5).setBackgroundResource(R.drawable.button_normal);
button_list.get(5).setTag("0");
}
break;
case button6:
if(button_list.get(6).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(6).getText().toString());
is_clicked= (String)button_list.get(6).getTag();
if ("0".equals(is_clicked)){
button_list.get(6).setBackgroundResource(R.drawable.button_pressed);
button_list.get(6).setTag("1");
}else{
button_list.get(6).setBackgroundResource(R.drawable.button_normal);
button_list.get(6).setTag("0");
}
break;
case button7:
if(button_list.get(7).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(7).getText().toString());
is_clicked= (String)button_list.get(7).getTag();
if ("0".equals(is_clicked)){
button_list.get(7).setBackgroundResource(R.drawable.button_pressed);
button_list.get(7).setTag("1");
}else{
button_list.get(7).setBackgroundResource(R.drawable.button_normal);
button_list.get(7).setTag("0");
}
break;
case button8:
if(button_list.get(8).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(8).getText().toString());
is_clicked= (String)button_list.get(8).getTag();
if ("0".equals(is_clicked)){
button_list.get(8).setBackgroundResource(R.drawable.button_pressed);
button_list.get(8).setTag("1");
}else{
button_list.get(8).setBackgroundResource(R.drawable.button_normal);
button_list.get(8).setTag("0");
}
break;
case button9:
if(button_list.get(9).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(9).getText().toString());
is_clicked= (String)button_list.get(9).getTag();
if ("0".equals(is_clicked)){
button_list.get(9).setBackgroundResource(R.drawable.button_pressed);
button_list.get(9).setTag("1");
}else{
button_list.get(9).setBackgroundResource(R.drawable.button_normal);
button_list.get(9).setTag("0");
}
break;
case button10:
if(button_list.get(10).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(10).getText().toString());
is_clicked= (String)button_list.get(10).getTag();
if ("0".equals(is_clicked)){
button_list.get(10).setBackgroundResource(R.drawable.button_pressed);
button_list.get(10).setTag("1");
}else{
button_list.get(10).setBackgroundResource(R.drawable.button_normal);
button_list.get(10).setTag("0");
}
break;
Is there a way I can use a general variable to access each case so if I make a change I won't need to make it 50 times for each button? I thought about doing something like this:
public void ButtonOnClick(View v) {
switch (v.getId()) {
case button[i]
if(button_list.get(i).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(i).getText().toString());
is_clicked= (String)button_list.get(i).getTag();
if ("0".equals(is_clicked)){
button_list.get(i).setBackgroundResource(R.drawable.button_pressed);
button_list.get(i).setTag("1");
}else{
button_list.get(i).setBackgroundResource(R.drawable.button_normal);
button_list.get(i).setTag("0");
}
break;
Would this work? Or is there something else I need to do so I don't need to make 50 changes to my code every time I decide I want to change the OnClick method?
I'm going to assume that ButtonOnClick is the click handler for the buttons. I'm going to further assume that for any i, the contents of button_list.get(i) is the button. If that's right, then the argument v is the same Button object stored in the list. You can then reduce your entire handler to:
public void ButtonOnClick(View v) {
Button btn = (Button) v;
if (btn.getTag().equals("0")) {
enter_txt.setText(enter_txt.getText()+btn.getText().toString());
}
is_clicked= (String) btn.getTag();
if ("0".equals(is_clicked)){
btn.setBackgroundResource(R.drawable.button_pressed);
btn.setTag("1");
}else{
btn.setBackgroundResource(R.drawable.button_normal);
btn.setTag("0");
}
}
As an aside, you might consider using Boolean objects instead of String objects as the button tags.
First you should never copy paste code, try to extract your code in another method.
If you are using a arraylist where you store all your buttons, you can probably use the ArrayList.indexOf(Object o)
So just replace your ButtonOnClick method with this one, you can handle other buttons behavior easily too in the future :
public void ButtonOnClick(View v) {
if(button_list.contains(v))
{
setButtonBackground(button_list.indexOf(v));
}
}
private setButtonBackground(int buttonNumber)
{
Button myButton = (Button) button_list.get(buttonNumber);
if(myButton.getTag().equals("0"))
{
enter_txt.setText(enter_txt.getText()+myButton.getText().toString());
}
is_clicked = (String) myButton.getTag();
if ("0".equals(is_clicked))
{
myButton.setBackgroundResource(R.drawable.button_pressed);
myButton.setTag("1");
}
else
{
myButton.setBackgroundResource(R.drawable.button_normal);
myButton.setTag("0");
}
}
When you are developping instead of copy and paste code, just use the shortcut extract to a method so you will get something like this and will save yourself time.
[...]
case button1:
setButtonBackground(1);
break;
case button2:
setButtonBackground(2);
break;
[...]
Let me know if it works for you
Following the dev guide, I can add a method to a button using in the XML. This calls the 'buttonPress' method in my activity. If I apply the same method to multiple buttons, how can I determine the identity of the button that has been clicked?
Use getId() method. It returnes the int id that you can compare to the id from resources.
It is very convenient to use switch statement like this:
public void buttonPress(View v) {
switch (v.getId()) {
case R.id.button_one:
// do something
break;
case R.id.button_two:
// do something else
break;
case R.id.button_three:
// i'm lazy, do nothing
break;
}
}
So, I'm trying to set up a series of checkboxes that, when clicked, will add a number, depending on the checkbox to either the int variable attackTotal or damageTotal, which will in turn be shown in some textviews.
Currently, however, clicking the top checkbox acts as if I had clicked both it, and every checkbox after it in the switch statement. The second clickbox seems to activate itself and all the following ones as well, etcetera etcetera...
So here's the code I've gotten together.
public void onCheckboxClicked(View v) {
// Is the view now checked?
boolean checked = ((CheckBox) v).isChecked();
// Check which checkbox was clicked
switch(v.getId()) {
case R.id.checkBox1:
if (checked)
{
flankAttack=2;
}
else
{
flankAttack=0;
}
case R.id.checkBox2:
if (checked)
{
pbs=1;
}
else
{
pbs=0;
}
all the way down to..
case R.id.checkBox10:
if (checked)
{
attackTotal=attack+flankAttack+pbs;
damageTotal=damage+pbs;
TextView textView = (TextView) findViewById(R.id.textView2);
TextView textView2 = (TextView) findViewById(R.id.textView4);
textView.setText(Integer.toString(attackTotal));
textView2.setText(Integer.toString(damageTotal));
}
else
{
attackTotal=attack+flankAttack+pbs;
damageTotal=damage+pbs;
TextView textView = (TextView) findViewById(R.id.textView2);
TextView textView2 = (TextView) findViewById(R.id.textView4);
textView.setText(Integer.toString(attackTotal));
textView2.setText(Integer.toString(damageTotal));
}
I only started trying to figure out this programming stuff on Friday, so be gentle.
just before case R.id.checkBox2: you need to have a break; to tell the program to break from the switch statement. Otherwise anything meeting R.id.checkBox1 just continue through and executes all the logic you have for R.id.checkBox2 as well. (You need break; before all your other cases as well).
You forgot adding
break;
in the end of every case.
You forgot the break statement after each case. It is falling through to the next case each time.
Also, I strongly recommend not to put more than a few lines of code in a case statement, else it gets ugly very fast and hard to work with. Instead, pull out each case into its own method. Usually it will be easy to come up with a good name for each that is self-documenting:
switch (foo) {
case 0:
do();
lots();
of();
things();
break;
case 1:
do();
other();
things();
break;
case 2:
if (ugly)
{
this_gets();
messy();
quickly();
}
else
{
we_could();
do_better();
}
break;
}
Becomes:
void do_case_0() {
do();
lots();
of();
things();
}
void do_case_1() {
do();
other();
things();
}
void do_case_2() {
if (ugly)
{
this_gets();
messy();
quickly();
}
else
{
we_could();
do_better();
}
}
// ...
switch (foo) {
case 0: do_case_0(); break;
case 1: do_case_1(); break;
case 2: do_case_2(); break;
}
Instead of have to call separate method for each button, would it be possible to just use one method and check which button it was that was clicked by it's id?
I'm using the simple way like android:onClick="activateButtons"
public void activateButtons(View v)
{
if(?? == btn_1)
{
Code for button with ID = btn_1
}
if(?? == btn_1)
{
Code for button with ID = btn_2
}
}
You can do that if you tag the buttons with an Id. Set the android:id="#+id/yourbuttonid" in the xml file. Then do if(v.getId() == R.id.yourbuttonid) in the onClick method.
Yes it is possible. You can assign specific ids(eg: button1) for buttons and then call v.getId() in activateButtons(onclick method) to check if it is equal to R.id.button1 and then do your operation there.
Cheers,
Richie
public void onClickKeyPad(View view)
{
EditText text = (EditText)findViewById(R.id.text_password);
switch(view.getId())
{
case R.id.button_0:
text.setText(text.getText() + "0");
break;
case R.id.button_1:
text.setText(text.getText() + "1");
break;
case R.id.button_cancel:
finish();
break;
default:
break;
}
}