为什么我的应用程序崩溃(复选框)

时间:2016-02-27 12:55:42

标签: java android-studio checkbox crash uncheck

我正在尝试在android studio(java)中创建一个应用程序,但我的复选框有一点问题。每次当我取消选中该复选框时,我的应用程序都会崩溃。我们的意图是,当选中一个复选框时,整数存储在一个arraylist中,一个随机数发生器从数组中选择一个整数并显示它。谁能告诉我我做错了什么。

.java文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="21dp"
        android:id="@+id/number_D1"
        android:onClick="selectNumber"
        android:layout_below="@+id/text_double"
        android:layout_centerHorizontal="true"
        android:checked="false" />


    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="21dp"
        android:id="@+id/number_D2"
        android:onClick="selectNumber"
        android:layout_below="@+id/number_D1"
        android:layout_centerHorizontal="true"
        android:checked="false"
        android:visibility="visible" />


    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="21dp"
        android:id="@+id/number_D3"
        android:onClick="selectNumber"
        android:layout_below="@+id/number_D2"
        android:checked="false"
        android:layout_centerHorizontal="true"
        android:visibility="visible" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="21dp"
        android:id="@+id/number_D4"
        android:onClick="selectNumber"
        android:layout_below="@+id/number_D3"
        android:checked="false"
        android:layout_centerHorizontal="true"
        android:visibility="visible" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="21dp"
        android:onClick="selectNumber"
        android:layout_below="@+id/number_D4"
        android:checked="false"
        android:layout_centerHorizontal="true"
        android:id="@+id/number_D5"
        android:visibility="visible" />
</RelativeLayout>

.XML文件

    02-27 14:15:33.540 1024-1024/com.developer.sven.dartworkout20 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.developer.sven.dartworkout20, PID: 1024
                                                                                    java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
                                                                                        at android.view.View.performClick(View.java:4438)
                                                                                        at android.widget.CompoundButton.performClick(CompoundButton.java:100)
                                                                                        at android.view.View$PerformClick.run(View.java:18422)
                                                                                        at android.os.Handler.handleCallback(Handler.java:733)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                        at android.os.Looper.loop(Looper.java:136)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                        at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                                        at dalvik.system.NativeStart.main(Native Method)
                                                                                     Caused by: java.lang.reflect.InvocationTargetException
                                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                        at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
                                                                                        at android.view.View.performClick(View.java:4438) 
                                                                                        at android.widget.CompoundButton.performClick(CompoundButton.java:100) 
                                                                                        at android.view.View$PerformClick.run(View.java:18422) 
                                                                                        at android.os.Handler.handleCallback(Handler.java:733) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                        at android.os.Looper.loop(Looper.java:136) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                        at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                        at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                        at dalvik.system.NativeStart.main(Native Method) 
                                                                                     Caused by: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 1
                                                                                        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                                        at java.util.ArrayList.remove(ArrayList.java:403)
                                                                                        at com.developer.sven.dartworkout20.PreferenceDartThrow.selectNumber(PreferenceDartThrow.java:38)
                                                                                        at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                        at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
                                                                                        at android.view.View.performClick(View.java:4438) 
                                                                                        at android.widget.CompoundButton.performClick(CompoundButton.java:100) 
                                                                                        at android.view.View$PerformClick.run(View.java:18422) 
                                                                                        at android.os.Handler.handleCallback(Handler.java:733) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                        at android.os.Looper.loop(Looper.java:136) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                        at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                        at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                        at dalvik.system.NativeStart.main(Native Method) 

错误日志

Sub find_cell()
Dim find_cell As Range

Set ws = Sheets("TempList3")

stop_loop = False

Do Until stop_loop = True
    Set find_cell = ws.Cells.Find(What:=Sheets("Sheet1").ComboBox2.Value, LookAt:=xlWhole)
        If Not find_cell Is Nothing Then
           ws.Rows(find_cell.Row).EntireRow.Delete
        Else
           stop_loop = True
        End If
Loop
End Sub

1 个答案:

答案 0 :(得分:0)

您应该仔细查看例外java.lang.IndexOutOfBoundsException: Invalid index 2, size is 1

意味着在max index为1时尝试使用索引2。

Arraylist index以0开头,您的ID为1,因此您需要按ID-1删除,代码

public void selectNumber(View view) {
    boolean numberChecked = ((CheckBox) view).isChecked();
    switch (view.getId()) {
        case R.id.number_D1:
            if (numberChecked) {
                selection.add(1);
            } else {
                selection.remove(0);
            }
            break;

        case R.id.number_D2:
            if (numberChecked) {
                selection.add(2);
            } else {
                selection.remove(1);
            }
            break;

        case R.id.number_D3:
            if (numberChecked) {
                selection.add(3);
            } else {
                selection.remove(2);
            }
            break;

        case R.id.number_D4:
            if (numberChecked) {
                selection.add(4);
            } else {
                selection.remove(3);
            }
            break;

        case R.id.number_D5:
            if (numberChecked) {
                selection.add(5);
            } else {
                selection.remove(4);
            }
            break;
  }
}
}
相关问题