简体   繁体   中英

Android checkbox check for null to avoid app crash

I am very newbie on android studio and java. I have made a simple page which contain check boxes, and a button to print out in a toast message the checked box selected. The problem is if i clicked on submit button without selecting anything,the app stop working and crash. What i want to achieve is if i didn't select anything and i click on submit button it should send me a toast message "Please select a box" without making the app crash.

XML code:

<?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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:text="Favorite Courses: "
    android:textSize="20sp"
    android:layout_marginTop="30dp"
    android:layout_below="@+id/txDate"
    android:id="@+id/fvCourses"/>
<CheckBox
    android:id="@+id/cbCs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/fvCourses"
    android:layout_alignBottom="@+id/fvCourses"
    android:text="Computer Science" />
<CheckBox
    android:id="@+id/cbAcc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/fvCourses"
    android:layout_below="@+id/cbCs"
    android:text="Accounting" />
<CheckBox
    android:id="@+id/cbGraphic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/fvCourses"
    android:layout_below="@+id/cbAcc"
    android:text="Graphic Designer" />
<Button
    android:id="@+id/btnapply"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radioGroup"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="330dp"
    android:text="Confirm" />
    </RelativeLayout>

Java code:

package test.cb.app;

import androidx.appcompat.app.AppCompatActivity;

import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;
import android.widget.CheckBox;
public class MainActivity extends AppCompatActivity {

     TextView textView;
    CheckBox cbCs,cbAcc,cbGraphic;

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

        cbCs = findViewById(R.id.cbCs);
        cbAcc = findViewById(R.id.cbAcc);
        cbGraphic = findViewById(R.id.cbGraphic);

        Button btnapply = findViewById(R.id.btnapply);
        btnapply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                StringBuffer result = new StringBuffer();
                result.append("Computer Science check : ").append(cbCs.isChecked());
                result.append("\nAccounting check : ").append(cbAcc.isChecked());
                result.append("\nGraphic check :").append(cbGraphic.isChecked());
                Toast.makeText(MainActivity.this, result.toString(),
                        Toast.LENGTH_LONG).show();
            }
        });
        }

            }

I expect that i have to check for null values on the submit button.

What would be the code to achieve that?

Any help would be greatly appriciated.

Thank you!

As per your question, this code would work if you add some condition checks in the button click event. check the code updated, also you had some unwanted id in your xml file.

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;
import android.widget.CheckBox;

import androidx.appcompat.app.AppCompatActivity;

public class CheckActivity extends AppCompatActivity {

    TextView textView;
    CheckBox cbCs,cbAcc,cbGraphic;

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

        cbCs = findViewById(R.id.cbCs);
        cbAcc = findViewById(R.id.cbAcc);
        cbGraphic = findViewById(R.id.cbGraphic);

        Button btnapply = findViewById(R.id.btnapply);
        btnapply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               if(!cbCs.isChecked() &&!cbAcc.isChecked() && !cbGraphic.isChecked() ){
                   Toast.makeText(CheckActivity.this, "Select Any",
                           Toast.LENGTH_LONG).show();

               }
                else{
                    StringBuffer result = new StringBuffer();
                result.append("Computer Science check : ").append(cbCs.isChecked());
                result.append("\nAccounting check : ").append(cbAcc.isChecked());
                result.append("\nGraphic check :").append(cbGraphic.isChecked());
                Toast.makeText(CheckActivity.this, result.toString(),
                        Toast.LENGTH_LONG).show();
            }}
        });
    }

}

Also you make the following changes to your XML page.

<?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"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="Favorite Courses: "
        android:textSize="20sp"
        android:layout_marginTop="30dp"
        android:id="@+id/fvCourses"/>
    <CheckBox
        android:id="@+id/cbCs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/fvCourses"
        android:layout_alignBottom="@+id/fvCourses"
        android:text="Computer Science" />
    <CheckBox
        android:id="@+id/cbAcc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/fvCourses"
        android:layout_below="@+id/cbCs"
        android:text="Accounting" />
    <CheckBox
        android:id="@+id/cbGraphic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/fvCourses"
        android:layout_below="@+id/cbAcc"
        android:text="Graphic Designer" />
    <Button
        android:id="@+id/btnapply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="330dp"
        android:text="Confirm" />
</RelativeLayout>

just put into if else condition

package test.cb.app;

    import androidx.appcompat.app.AppCompatActivity;

    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.Button;
    import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity {

TextView textView;
CheckBox cbCs,cbAcc,cbGraphic;

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

    cbCs = findViewById(R.id.cbCs);
    cbAcc = findViewById(R.id.cbAcc);
    cbGraphic = findViewById(R.id.cbGraphic);

    Button btnapply = findViewById(R.id.btnapply);
    btnapply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            StringBuffer result = new StringBuffer();
            result.append("Computer Science check : ").append(cbCs.isChecked());
            result.append("\nAccounting check : ").append(cbAcc.isChecked());
            result.append("\nGraphic check :").append(cbGraphic.isChecked());
            if(result.equals("")) {
                Toast.makeText(MainActivity.this, "no checkbox is selected",
                        Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(MainActivity.this, result.toString(),
                        Toast.LENGTH_LONG).show();
            }
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM