1

所以我有一个微调器,一个按钮和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" />

日志文件:

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
4

1 回答 1

1

当您在布局中移动了两个元素(交换了它们的位置)时,通常会发生这类错误。Eclipse 中的 Project --> Clean 通常会解决此问题,这会使 Android 重新构建 R 类。

于 2013-06-23T13:13:48.377 回答