簡體   English   中英

Android 復選框檢查 null 以避免應用程序崩潰

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

我是 android 工作室和 java 的新手。 我制作了一個簡單的頁面,其中包含復選框和一個按鈕,用於在 toast 消息中打印出選中的復選框。 問題是如果我在沒有選擇任何內容的情況下單擊提交按鈕,應用程序將停止工作並崩潰。 我想要實現的是,如果我沒有 select 任何東西並且我點擊提交按鈕它應該向我發送一條祝酒消息“請 select 一個盒子”而不會使應用程序崩潰。

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代碼:

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();
            }
        });
        }

            }

我希望我必須檢查提交按鈕上的 null 值。

實現這一目標的代碼是什么?

任何幫助將不勝感激。

謝謝!

根據您的問題,如果您在按鈕單擊事件中添加一些條件檢查,則此代碼將起作用。 檢查更新的代碼,您的 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();
            }}
        });
    }

}

此外,您還對 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>

只需放入 if else 條件

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;

公共 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