-2

I'm trying to make an app which, in this certain activity I have a checkbox that if isChecked then show a button to the user, which will be used to launch the camera. I've tried looking for examples in other overflow topics and could find some, but everytime I try them, my app crashes. Could you please help me saying what am I doing wrong? Much thanks in advance.

package com.example.report;


public class started_report extends AppCompatActivity {

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

        final Button buttonCamera;
        buttonCamera = (Button)this.findViewById(R.id.buttonCamera);

        CheckBox checkbox = (CheckBox)this.findViewById(R.id.checkbox);
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            buttonCamera.setVisibility(View.VISIBLE);
        }
    });
}

public void openCamera(View view){
    //For posterior use to call the camera
}

}

This is my errorlog: Is it saying "on a null object reference" because I'm using void?

09-11 00:20:58.199 10913-10913/com.example.report E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.report, PID: 10913
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.report/com.example.report.started_report}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
      at android.app.ActivityThread.access$800(ActivityThread.java:151)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5254)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
      at com.example.relatoriodeobras.started_alvara.onCreate(started_alvara.java:21)
      at android.app.Activity.performCreate(Activity.java:5990)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5254) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

my xml layout is here:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.report.started_report">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/started_report"
        android:id="@+id/textView3"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/placadeobra"
        android:id="@+id/checkBox"
        android:singleLine="false"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/camera"
        android:id="@+id/buttonCamera"
        android:layout_above="@+id/radioGroup2"
        android:layout_centerHorizontal="true"
        android:visibility="gone"
        android:onClick="openCamera"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/projetoexecutivo"
        android:id="@+id/checkBox2"
        android:layout_below="@+id/checkBox"
        android:layout_centerHorizontal="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="false" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup2"
        android:layout_below="@+id/checkBox2"
        android:layout_alignRight="@+id/button"
        android:layout_alignEnd="@+id/button"
        android:layout_marginTop="49dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/RT"
            android:id="@+id/textView5"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="39dp"
            android:text="@string/ART"
            android:id="@+id/radioButton3"
            android:layout_marginTop="35dp" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="36dp"
            android:text="@string/RRT"
            android:id="@+id/radioButton4" />

    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/notobs"
        android:id="@+id/textView4"
        android:layout_below="@+id/radioGroup2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="39dp" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView4"
        android:layout_alignParentLeft="true"
        android:inputType="none"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buttonfinalize"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/editText"
        android:layout_alignEnd="@+id/editText" />

</RelativeLayout>
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41

2 Answers2

1

I suggest you use setOnClickListener method instead of, like this

final Button buttonCamera;
buttonCamera = (Button)this.findViewById(R.id.buttonCamera);

final CheckBox checkbox = (CheckBox)this.findViewById(R.id.checkbox);    

checkbox.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {

        if (checkBox.isChecked()) {
            buttonCamera.setVisibility(View.VISIBLE);
        }
        else {
            buttonCamera.setVisibility(View.INVISIBLE);
        }

      }
    });
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
0

This is my errorlog: Is it saying "on a null object reference" because I'm using void?

You get this message because a reference variable is null. In this case it is the CheckBox. Be sure you call findViewById() to initialize your variable before calling methods on it.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268