I'm trying to make a app in android studio(java) but I have a problem with my checkbox. Every time when I unchecked the checkbox my app will crash. It is the intention that when a checkbox is checked, the integer is stored in a arraylist and a randomizer pick one of those integers out of the array and displays it. Can somebody tell me what I am doing wrong.
.java file
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 file
<?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>
error log
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)
When you use remove(1),, you are removing the entry at position "1", not the entry with value "1". If you want to remove entry with value "1", you have to convert to an object first:
selection.remove(new Integer(1));