Android - isChecked时的复选框,显示按钮

时间:2016-09-11 00:24:48

标签: android

我正在尝试创建一个应用程序,在这个特定的活动中我有一个复选框,如果isChecked然后向用户显示一个按钮,它将用于启动相机。 我已经尝试在其他溢出主题中查找示例并且可以找到一些,但每次我尝试它们时,我的应用程序崩溃了。 你能帮我说一下我做错了吗? 非常感谢提前。

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
}

}

这是我的错误日志: 它是说“在空对象引用上”因为我使用的是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) 

我的xml布局在这里:

<?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>

2 个答案:

答案 0 :(得分:1)

我建议你使用setOnClickListener方法,而不是像这样

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

      }
    });

答案 1 :(得分:0)

  

这是我的错误日志:是否说“在空对象引用上”因为我使用的是void?

您收到此消息是因为引用变量为null。在这种情况下,它是CheckBox。确保在调用方法之前调用findViewById()来初始化变量。