This is just a basic Q&A app.
I get the error when I tried to press a radiobutton
on the app interface.
Here is my main class:
public class Balance extends AppCompatActivity {
private int score = 0; //answer score
private int PreviousScore;
private int i = 0;
private Vibrator myVib;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_balance);
final TextView Score = (TextView) findViewById(R.id.Score);
Button b = (Button)findViewById(R.id.button); //button
myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
question(); //call params for question
//button listener, when button clicked, produce output on textfield "Score"
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myVib.vibrate(70);
PreviousScore = score;
//Score.setText(String.valueOf(action()));
//Score.setText(score);
i++;
question();
RadioGroup mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
mRadioGroup.clearCheck();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_balance, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void question(){
//basically assigning strings to radiobuttons here
}
private void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
myVib.vibrate(50);
// Check which radio button was clicked
switch(view.getId()) {
case R.id.AnswerA:
if (checked)
score = PreviousScore + 0;
break;
case R.id.AnswerB:
if (checked)
score = PreviousScore + 1;
break;
case R.id.AnswerC:
if (checked)
score = PreviousScore + 2;
break;
}
}
private String action(){
String action = null;
if (score <= 16)
action= "Low Risk";
else if(score > 16 & score <= 32)
action = "Medium Risk";
else if(score > 32 & score <= 48)
action = "High Risk";
else if(score > 48 & score <= 64)
action = "Very High Risk";
return action;
}
}
Here is my content view
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/Question"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button"
android:layout_marginBottom="132dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/Score"
android:text="score goes here"
android:layout_above="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/radioGroup"
android:layout_above="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/AnswerA"
android:layout_below="@+id/Question"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="41dp"
android:checked="false"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/AnswerB"
android:layout_below="@+id/AnswerA"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/AnswerC"
android:layout_below="@+id/AnswerB"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/AnswerD"
android:layout_below="@+id/AnswerC"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Text"
android:id="@+id/Next_Text"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="58dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/currentscore"
android:layout_below="@+id/Next_Text"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
Logcat:
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: FATAL EXCEPTION: main
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: Process: com.example.user.tinetti, PID: 9082
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: java.lang.IllegalStateException: Could not find method onRadioButtonClicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatRadioButton with id 'AnswerB'
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:310)
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:269)
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: at android.view.View.performClick(View.java:5198)
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: at android.widget.CompoundButton.performClick(CompoundButton.java:122)
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21147)
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
02-20 14:14:46.484 9082-9082/com.example.user.tinetti E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
You have to set attribute OnClick for all RadioButtons.Android is triggering an event may be when you are clicking on radio button.It is unable to find onClick method.It may be in recent version(API 23).It is my practical answer android:onClick="onRadioButtonClicked" add this to your radio button for both. Define a onRadioButtonClicked method in your activity which is linked to your layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With