mardi 24 avril 2018

CheckBox updating Progress Bar does not load

Application simply says "Unfortunately stopped working" on device. Each checkbox needs to update the progress bar percentage. For some reason the app will not deploy and I have no idea if the code I am using is functional or appropriate. Any advice will be appreciated very much on how to get this concept functioning!

package com.example.thomas.progressbar;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ProgressBar;

public class ProgressBars extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_progress_bars);


    final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
    final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
    final CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
    final CheckBox checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
    final CheckBox checkBox5 = (CheckBox) findViewById(R.id.checkBox5);


    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    final SharedPreferences.Editor editor = preferences.edit();
    if (preferences.contains("checkbox1") && preferences.getBoolean("checkbox1", false) == true) {
        checkBox.setChecked(true);
    } else {
        checkBox.setChecked(false);

    }
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (checkBox.isChecked()) {
                editor.putBoolean("checkbox1", true);
                progressBar.setProgress(20);
                editor.apply();
            } else {
                editor.putBoolean("checkbox1", false);
                progressBar.setProgress(-20);
                editor.apply();
            }
        }
    });


    if (preferences.contains("checkbox2") && preferences.getBoolean("checkbox2", false) == true) {
        checkBox2.setChecked(true);
    } else {
        checkBox2.setChecked(false);

    }
    checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (checkBox2.isChecked()) {
                editor.putBoolean("checkbox2", true);
                progressBar.setProgress(20);
                editor.apply();
            } else {
                editor.putBoolean("checkbox2", false);
                progressBar.setProgress(-20);
                editor.apply();
            }
        }
    });


    if (preferences.contains("checkbox3") && preferences.getBoolean("checkbox3", false) == true) {
        checkBox3.setChecked(true);
    } else {
        checkBox3.setChecked(false);

    }
    checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (checkBox3.isChecked()) {
                editor.putBoolean("checkbox3", true);
                progressBar.setProgress(20);
                editor.apply();
            } else {
                editor.putBoolean("checkbox3", false);
                progressBar.setProgress(-20);
                editor.apply();
            }
        }
    });

    if (preferences.contains("checkbox4") && preferences.getBoolean("checkbox4", false) == true) {
        checkBox4.setChecked(true);
    } else {
        checkBox4.setChecked(false);

    }
    checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (checkBox4.isChecked()) {
                editor.putBoolean("checkbox4", true);
                progressBar.setProgress(20);
                editor.apply();
            } else {
                editor.putBoolean("checkbox4", false);
                progressBar.setProgress(-20);
                editor.apply();
            }
        }
    });

    if (preferences.contains("checkbox5") && preferences.getBoolean("checkbox5", false) == true) {
        checkBox5.setChecked(true);
    } else {
        checkBox5.setChecked(false);

    }
    checkBox5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (checkBox5.isChecked()) {
                editor.putBoolean("checkbox5", true);
                progressBar.setProgress(20);
                editor.apply();
            } else {
                editor.putBoolean("checkbox5", false);
                progressBar.setProgress(-20);
                editor.apply();
            }
        }
    });

}
}

Here is the XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.thomas.progressbar.ProgressBars">


<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="20dp"
    android:layout_marginTop="84dp"
    android:text="CheckBox" />

<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/checkBox"
    android:layout_below="@+id/checkBox"
    android:layout_marginStart="12dp"
    android:layout_marginTop="48dp"
    android:text="CheckBox" />

<CheckBox
    android:id="@+id/checkBox3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/checkBox2"
    android:layout_centerVertical="true"
    android:text="CheckBox" />

<CheckBox
    android:id="@+id/checkBox4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox" />

<CheckBox
    android:id="@+id/checkBox5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/checkBox3"
    android:layout_below="@+id/checkBox3"
    android:layout_marginTop="64dp"
    android:text="CheckBox" />

<Button
    android:id="@+id/compoundButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/checkBox5"
    android:layout_marginStart="27dp"
    android:layout_toEndOf="@+id/progressBar"
    android:text="Button" />

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/compoundButton"
    android:layout_marginStart="62dp"
    android:layout_marginTop="12dp"
    android:layout_toEndOf="@+id/checkBox5" />
</RelativeLayout>




Aucun commentaire:

Enregistrer un commentaire