所以我有一个微调器,一个按钮和edittext,根据在微调器上选择的项目,以及在edittext中哪个值在按钮单击时发生。它曾经完美地工作,现在它每次我点击按钮时突然崩溃。我不知道我做了什么毁了它所以我很恳请你帮忙。我已经被困了好几个小时,我只是无法指出破坏代码的原因。
按钮点击方法(正如您在案例1中看到的那样,我在注释中添加了共享首选项,以确保这不是运行代码的内容):
public void submitQuantityButton (View v){
Button submitButton = (Button)findViewById(R.id.submitButton);
final Spinner sItems = (Spinner)findViewById(R.id.spinner1);
final Context context = this;
final CheckBox cb4 = (CheckBox) findViewById(R.id.checkBox4);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int position = sItems.getSelectedItemPosition();
EditText quantityEditText = (EditText)findViewById(R.id.editText1);
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:
String items = quantityEditText.getText().toString();
cb4.setText("Elaborate Totem (" + items + "/250)");
//saveItemQuantity("cb4", cb4.getText().toString());
break;
}
}
});
}
按钮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="submitQuantityButton" />
Logcat文件:
06-23 15:05:29.276: E/AndroidRuntime(22681): FATAL EXCEPTION: main
06-23 15:05:29.276: E/AndroidRuntime(22681): java.lang.IllegalStateException: Could not execute method of the activity
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.view.View$1.onClick(View.java:3599)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.view.View.performClick(View.java:4204)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.view.View$PerformClick.run(View.java:17355)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.os.Handler.handleCallback(Handler.java:725)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.os.Handler.dispatchMessage(Handler.java:92)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.os.Looper.loop(Looper.java:137)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-23 15:05:29.276: E/AndroidRuntime(22681): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 15:05:29.276: E/AndroidRuntime(22681): at java.lang.reflect.Method.invoke(Method.java:511)
06-23 15:05:29.276: E/AndroidRuntime(22681): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-23 15:05:29.276: E/AndroidRuntime(22681): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-23 15:05:29.276: E/AndroidRuntime(22681): at dalvik.system.NativeStart.main(Native Method)
06-23 15:05:29.276: E/AndroidRuntime(22681): Caused by: java.lang.reflect.InvocationTargetException
06-23 15:05:29.276: E/AndroidRuntime(22681): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 15:05:29.276: E/AndroidRuntime(22681): at java.lang.reflect.Method.invoke(Method.java:511)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.view.View$1.onClick(View.java:3594)
06-23 15:05:29.276: E/AndroidRuntime(22681): ... 11 more
06-23 15:05:29.276: E/AndroidRuntime(22681): Caused by: java.lang.ClassCastException: android.widget.Spinner cannot be cast to android.widget.Button
06-23 15:05:29.276: E/AndroidRuntime(22681): at com.example.gw2legendary.Bifrost.submitQuantityButton(Bifrost.java:967)
06-23 15:05:29.276: E/AndroidRuntime(22681): ... 14 more
答案 0 :(得分:1)
当你在布局中移动了两个元素(切换它们的位置)时,通常会发生这类错误。一个项目 - &gt; Eclipse中的Clean通常会解决这个问题,这会让Android重建R级。