samedi 27 février 2016

why is my app crashing (checkbox)

I'm trying to make a app in android studio(java) but i have a little problem whit my checkbox. every time when I unchecked the checkbox my app will crash. 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://ift.tt/nIICcg"
    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" />




Aucun commentaire:

Enregistrer un commentaire