这只是一个基本的Q& A应用。
当我尝试在应用界面上按radiobutton
时出现错误。
这是我的主要课程:
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;
}
}
这是我的内容视图
<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)
答案 0 :(得分:6)
你必须为所有RadioButtons.Android设置属性OnClick,当你点击单选按钮时可能会触发一个事件。它无法找到onClick方法。它可能是最新版本(API 23)。它是我的实际答案 android:onClick =&#34; onRadioButtonClicked&#34; 将此添加到您的单选按钮。 在您的活动中定义 onRadioButtonClicked 方法,该方法与您的布局相关联。
答案 1 :(得分:1)
您必须将private void onRadioButtonClicked
设置为public void onRadioButtonClicked