我想根据选择的微调器上的哪个项目对按钮按下执行某个操作。这是我到目前为止所得到的:
public void submitButton (View v){
Button b1 = (Button)findViewById(R.id.submitButton);
final Spinner s1 = (Spinner)findViewById(R.id.spinner1);
final Context context = this;
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int position = s1.getSelectedItemPosition();
switch (position){
case 0:
AlertDialog.Builder spinnerErrorBuilder = new AlertDialog.Builder(context);
spinnerErrorBuilder.setTitle("Warning");
spinnerErrorBuilder.setMessage("Please choose an item from the list");
spinnerErrorBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog spinnerError = spinnerErrorBuilder.create();
spinnerError.show();
break;
case 1:
break;
}
}
});
}
当我编译我的应用程序并单击按钮时,应用程序崩溃并返回到主要活动。无论我选择了哪个项目(0 或 1),应用程序仍然会崩溃。有人能告诉我哪里出错了吗?
按钮的 XML 代码:
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignLeft="@+id/checkBox25"
android:text="@string/addMaterial"
android:onClick="onClick" />
日志文件:
06-22 15:00:13.455: E/AndroidRuntime(23409): java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class com.example.gw2legendary.Bifrost for onClick handler on view class android.widget.Button with id 'submitButton'