mardi 24 septembre 2019

Unable to get the reference of CheckBox in android

I am using a CheckBox in my activity layout. But I don't know why I am not getting the refrence of the CheckBox. It shows NullPointerException everytime. I have used another checkbox in my dialog fragment but it was working fine. I don't know what is the cause of NullPointerException.

Here is xml code

 <CheckBox
        android:id="@+id/ccb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="8dp"
        android:text="Add Your Previous Amount\n with Current Amount "
        android:textColor="@color/batteryChargedBlue"
        android:textSize="13dp"
        android:fontFamily="@font/caviardreams_bolditalic"
        />

Here is the java code where I am using the checkbox and getting NullPointerException.

public class StartActivity extends AppCompatActivity {



//Declaring the CheckBox variable globally


CheckBox scb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    ...

       //Initializing in the onCreate() method
       scb = (CheckBox)findViewById(R.id.ccb);

    ...
}

//using it in the button clicked method to check whether checkbox is checked or not
public void setInitialMonthlyAmount(View view) {

      if(scb.isChecked()) { //Getting The NullPointerException Here Don't know why ??
                System.out.println("Checkbox is checked");
                //Code
            }else {
                //Code
            }
}


}

I think everything is correct. and I am using the same way to get the references of other views like TextViews, EditText etc and they are working fine. I don't what is wrong with this checkbox. Please Help!!!




Aucun commentaire:

Enregistrer un commentaire