-1

so I had a version of this working earlier. Unfortunately, I forgot to copy the working version on to my USB, so I've attempted to duplicate this at home. I have 5 directional radio buttons. Each time one is checked, it is supposed to change the text in a TextView to correspond with the associated direction. Unfortunately, this does not seem to be working. When attempting to run the app, it crashes.

The debugging screen also throws out something about null pointer exceptions. Also, in the xml code for the radio buttons, there is something about the method I am calling not being found in Main. Any help is appreciated.

<RadioButton
    android:text="@string/north"
    android:layout_width="79dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="12dp"
    android:layout_marginStart="12dp"
    android:layout_marginTop="29dp"
    android:id="@+id/north"
    android:onClick="onClick" />

<RadioButton
    android:text="@string/south"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/south" />

<RadioButton
    android:text="@string/east"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/east" />

<RadioButton
    android:text="@string/west"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/west" />

<RadioButton
    android:text="@string/center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/center" />

</RadioGroup>

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="22dp"
    android:id="@+id/speed"
    android:layout_below="@+id/Speed"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

Activity:

public class MainActivity extends AppCompatActivity {
    RadioButton n, s, e, w, c;
    // TextView text = (TextView)findViewById(R.id.text);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public View.OnClickListener optionOnClickListener;

    {
        optionOnClickListener = new OnClickListener() {

            public View v;

            public void onClick(View v) {
                this.v = v;
                TextView text = (TextView) findViewById(R.id.text);
                String str = null;
                n = (RadioButton) findViewById(R.id.north);
                s = (RadioButton) findViewById(R.id.south);
                e = (RadioButton) findViewById(R.id.east);
                w = (RadioButton) findViewById(R.id.west);
                c = (RadioButton) findViewById(R.id.center);


                // you can simply copy the string of clicked button.
                str = ((RadioButton) v).getText().toString();
                text.setText(str);

                // or, you can check each radiobutton and find which one is checked.
                if (n.isChecked()) {
                    text.setText("North");
                } else if (s.isChecked()) {
                    text.setText("South");
                } else if (e.isChecked()) {
                    text.setText("East");
                } else if (w.isChecked()) {
                    text.setText("West");
                } else if (c.isChecked()) {
                    text.setText("Center");
                }

            }

        };
    }

}
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
John Casey
  • 27
  • 2
  • 8

2 Answers2

0

Because you are setting the onClick method from XML this block that you have:

public View.OnClickListener optionOnClickListener;

{
    optionOnClickListener = new OnClickListener() {

        public View v;

 //delete above here
        public void onClick(View v) {
            this.v = v; //delete this
            TextView text = (TextView) findViewById(R.id.text);
            String str = null;
            n = (RadioButton) findViewById(R.id.north);
            s = (RadioButton) findViewById(R.id.south);
            e = (RadioButton) findViewById(R.id.east);
            w = (RadioButton) findViewById(R.id.west);
            c = (RadioButton) findViewById(R.id.center);


            // you can simply copy the string of clicked button.
            str = ((RadioButton) v).getText().toString();
            text.setText(str);

            // or, you can check each radiobutton and find which one is checked.
            if (n.isChecked()) {
                text.setText("North");
            } else if (s.isChecked()) {
                text.setText("South");
            } else if (e.isChecked()) {
                text.setText("East");
            } else if (w.isChecked()) {
                text.setText("West");
            } else if (c.isChecked()) {
                text.setText("Center");
            }

        }

//delete below here

    };
}

Should be:

//remove unneccesary variable and wrapper

public void onClick(View v) {
    ... // keep all the lines in this method except this.v = v;
}

After that it should compile, although one addition I would make is to do all of the findViewById(...)'s in onCreate instead of doing them on every click

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • If I remove the wrapper, I get a load of errors. What would the new code look like? – John Casey Feb 28 '17 at 03:45
  • Exactly as in my example... I don't know what more you could mean. Especially as you say you had this working earlier. What are the "load of errors"? – Nick Cardoso Feb 28 '17 at 03:46
  • Variable and wrapper are gone. It still crashes. I cannot pull my code from earlier. – John Casey Feb 28 '17 at 03:50
  • That's the thing. There are no red flags in the log. – John Casey Feb 28 '17 at 03:58
  • Does it compile? Is it a fragment? Are you using API 4 or less? is there something red in the code what will tell you the problem when you hover? The last thing that occurred to me is onClick might also be a reserved term so try changing the method name and XML to onRadioClick – Nick Cardoso Feb 28 '17 at 03:59
  • Okay, so I ran it with the debugger. This stood out: java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatRadioButton with id 'north' – John Casey Feb 28 '17 at 04:00
  • In your layout XML above, I assume there is a wrapper xml node around that? and it is activity_main.xml? Did you try changing the name as I mentioned in my last comment? – Nick Cardoso Feb 28 '17 at 04:03
  • Everything is in a wrapper – John Casey Feb 28 '17 at 04:08
0

Okay, so It turned out that I didn't even need the listener, as the library seemed to take care of it. Also, I think Nick Cardoso was correct when he said that onClick was reserved. It's been a long day. Thank you all for the help! Good night.

John Casey
  • 27
  • 2
  • 8