Android CheckBox NullPointer

时间:2016-03-07 14:49:28

标签: android

我有用于条件语句的复选框的代码。

CheckBox Cb_Ar,Cb_Db,Cb_Hb;
   Cb_Ar = (CheckBox) findViewById(R.id.check1);
    Cb_Db = (CheckBox) findViewById(R.id.check2);
    Cb_Hb = (CheckBox) findViewById(R.id.check3);
  String toPass="";


    if(Cb_Ar.isChecked()){
        toPass = "Arthritis";
    }

它给出了空指针异常

logcat的:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.CheckBox.isChecked()' on a null object reference

活动使用复选框,这是使用它的整个活动     包com.example.jello.letscook;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class showmenu extends AppCompatActivity {
    DatabaseHelper myDb;
    Spinner mainIng;
    EditText sub1,sub2,sub3,sub4,sub5,sub6;
    CheckBox Cb_Ar,Cb_Db,Cb_Hb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_showmenu);
    myDb = new DatabaseHelper(this);

     mainIng = (Spinner) findViewById(R.id.spinner);
     sub1 = (EditText) findViewById(R.id.sub1);
     sub2 = (EditText) findViewById(R.id.sub2);
     sub3 = (EditText) findViewById(R.id.sub3);
     sub4 = (EditText) findViewById(R.id.sub4);
     sub5 = (EditText) findViewById(R.id.sub5);
     sub6 = (EditText) findViewById(R.id.sub6);
    Cb_Ar = (CheckBox) findViewById(R.id.check1);
    Cb_Db = (CheckBox) findViewById(R.id.check2);
    Cb_Hb = (CheckBox) findViewById(R.id.check3);
    showList();


                    //   myDb.insertHB(true);
                     //  myDb.inert_Arth(true);
                       //myDb.inert_Dia(true);






    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

public void showList() {
    String toPass="";

    if(Cb_Ar.isChecked()){
        toPass = "Arthritis";
    }


    Cursor cursor = myDb.getData(toPass);
    String[] fromField = new String[] {DatabaseHelper.RECIPE_NAME};
    int[] toView = new int[] {R.id.textViewMenuSuggest};
    SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new SimpleCursorAdapter(this,R.layout.menushow,cursor,fromField,toView,0);
    ListView myList = (ListView) findViewById(R.id.selectView);
    myList.setAdapter(myCursorAdapter);

}
}

支票箱所在的XML代码:

   <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="15dp"
    android:paddingTop="16dp"
    android:background="#3A539B"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_starting_screen" tools:context=".StartingScreen">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#EEEEEE">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#3A539B">

            <TextView
                android:layout_marginTop="35dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Main Ingredient"
                android:id="@+id/textView2" />


            <Spinner
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:spinnerMode="dialog"
                android:id="@+id/spinner" />


            <RelativeLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/relLayout">

                <TextView
                    android:layout_marginTop="15dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="Sub Ingredients"
                    android:id="@+id/textView3" />

                <Button
                    android:layout_width="40dp"
                    android:layout_height="30dp"
                    android:layout_marginTop="10dp"
                    android:layout_gravity="center"
                    android:textAlignment="center"
                    android:text="+"
                    android:textSize="25dp"
                    android:background="#2ECC71"
                    android:layout_alignParentRight="true"
                    android:id="@+id/addText" />
            </RelativeLayout>

            <LinearLayout
                android:layout_marginTop="10dp"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center" >

                <LinearLayout
                    android:orientation="horizontal"
                    android:id="@+id/mLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:focusableInTouchMode="true">


                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent">

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="Sub Ingredient 1"
                            android:textStyle="italic"
                            android:textColorHint="#95A5A6"
                            android:id="@+id/sub1" />

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="gone"
                            android:hint="Sub Ingredient 2"
                            android:textStyle="italic"
                            android:textColorHint="#95A5A6"
                            android:id="@+id/sub2" />

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="gone"
                            android:hint="Sub Ingredient 3"
                            android:textStyle="italic"
                            android:textColorHint="#95A5A6"
                            android:id="@+id/sub3" />

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="gone"
                            android:hint="Sub Ingredient 4"
                            android:textStyle="italic"
                            android:textColorHint="#95A5A6"
                            android:id="@+id/sub4" />

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="gone"
                            android:hint="Sub Ingredient 5"
                            android:textStyle="italic"
                            android:textColorHint="#95A5A6"
                            android:id="@+id/sub5" />

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="gone"
                            android:hint="Sub Ingredient 6"
                            android:textStyle="italic"
                            android:textColorHint="#95A5A6"
                            android:id="@+id/sub6" />
                    </LinearLayout>

                </LinearLayout>

                <TextView
                    android:layout_marginTop="50dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="Health Condition"
                    android:id="@+id/textView" />

                <LinearLayout
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent">

                        <CheckBox
                            android:layout_width="100dp"
                            android:layout_height="wrap_content"
                            android:text="Arthritis"
                            android:textSize="15dp"
                            android:id="@+id/check1"
                            android:checked="false" />

                        <CheckBox
                            android:layout_width="100dp"
                            android:layout_height="wrap_content"
                            android:text="Diabetic"
                            android:textSize="15dp"
                            android:id="@+id/check2" />

                        <CheckBox
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Highblood"
                            android:textSize="15dp"
                            android:id="@+id/check3" />
                    </LinearLayout>

                </LinearLayout>

                <Button
                    android:layout_marginTop="80dp"
                    android:gravity="center"
                    android:layout_width="fill_parent"
                    android:onClick="nextScreen"
                    android:layout_height="match_parent"
                    android:background="#2ECC71"
                    android:layout_gravity="bottom"
                    android:text="Lets Cook!"
                    android:id="@+id/button" />

            </LinearLayout>

            </LinearLayout>

        </LinearLayout>


    </ScrollView>

1 个答案:

答案 0 :(得分:0)

有一切都好,只需用作

CheckBox Cb_Ar,Cb_Db,Cb_Hb;
   Cb_Ar = (CheckBox) view.findViewById(R.id.check1);
    Cb_Db = (CheckBox) view.findViewById(R.id.check2);
    Cb_Hb = (CheckBox) view.findViewById(R.id.check3);
  String toPass="";


    if(Cb_Ar.isChecked()){
        toPass = "Arthritis";
    }

如果您正在使用片段和任何对话框,那么

Cb_Ar = (CheckBox) dialog.findViewById(R.id.check1);
    Cb_Db = (CheckBox) dialog.findViewById(R.id.check2);
    Cb_Hb = (CheckBox) dialog.findViewById(R.id.check3);

它对我来说很好