简体   繁体   English

Android 复选框检查 null 以避免应用程序崩溃

[英]Android checkbox check for null to avoid app crash

I am very newbie on android studio and java.我是 android 工作室和 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.我制作了一个简单的页面,其中包含复选框和一个按钮,用于在 toast 消息中打印出选中的复选框。 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.我想要实现的是,如果我没有 select 任何东西并且我点击提交按钮它应该向我发送一条祝酒消息“请 select 一个盒子”而不会使应用程序崩溃。

XML code: 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"
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: Java代码:

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.我希望我必须检查提交按钮上的 null 值。

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.检查更新的代码,您的 xml 文件中还有一些不需要的 id。

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 页面进行了以下更改。

<?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只需放入 if else 条件

package test.cb.app; 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 {公共 class MainActivity 扩展 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();
            }
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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