我试图在android studio(java)中创建一个应用程序,但我的复选框有一点问题。每次当我取消选中该复选框时,我的应用程序都会崩溃。我们的意图是,当选中一个复选框时,整数存储在一个arraylist中,一个随机数发生器从数组中选择一个整数并显示它。谁能告诉我我做错了什么。
.java文件
package com.developer.sven.dartworkout20;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import java.util.ArrayList;
public class PreferenceDartThrow extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preference_dart_throw_page);
Intent preferenceDartThrowPageOpened = getIntent();
String previousActivity = preferenceDartThrowPageOpened.getExtras().getString("Pref");
}
ArrayList<Integer> selection = new ArrayList<Integer>();
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(1);
}
break;
case R.id.number_D2:
if (numberChecked) {
selection.add(2);
} else {
selection.remove(2);
}
break;
case R.id.number_D3:
if (numberChecked) {
selection.add(3);
} else {
selection.remove(3);
}
break;
case R.id.number_D4:
if (numberChecked) {
selection.add(4);
} else {
selection.remove(4);
}
break;
case R.id.number_D5:
if (numberChecked) {
selection.add(5);
} else {
selection.remove(5);
}
break;
}
}
}
.XML文件
<?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>
错误日志
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)
答案 0 :(得分:0)
当您使用remove(1)时,您将删除位置“1”的条目,而不是值为“1”的条目。如果要删除值为“1”的条目,则必须先转换为对象:
selection.remove(new Integer(1));
答案 1 :(得分:0)
您还可以使用String ArrayList并删除要直接删除的项目。 如果只有使用Integer的约束,则可以将该int转换为字符串,然后使用
进行解析来还原Integer.parseInt(str)
在需要时。该视频演示了如何使用字符串列表添加和删除复选框的列表项。 https://youtu.be/1H5-CBQWS-o 结帐 希望能帮助您解决问题。