2016-09-11 24 views
-2

我想做一个应用程序,在这个特定的活动中,我有一个复选框,如果isChecked然后显示一个按钮给用户,这将用于启动相机。 我试过寻找其他溢出主题的例子,可以找到一些,但每次我尝试他们,我的应用程序崩溃。 你能帮我说我做错了什么吗? 非常感谢提前。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 
} 

}

这是我的错误日志: 难道说:“在一个空对象引用”,因为我使用的是无效?

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> 
+0

“started_alvara.java:21” 什么是started_alvara.java的第21行? –

+0

它的started_report *,但它是我调用OnCheckedChangedListener checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() – HextechEnthusiast

+0

这一错误信息说问题出在一个名为started_alvara.java的文件中,请从这个文件中发布代码或者真正的错误消息 –

回答

0

这是我的错误日志:是不是说“上一个空对象引用”,因为我使用的是无效?

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

+0

但是我已经这么做了......这不是在做什么? CheckBox checkbox =(CheckBox)this.findViewById(R.id.checkbox); – HextechEnthusiast

+0

@HextechEnthusiast是的,你做到了。我正在根据不完整的信息提出建议。请提供我所要求的附加信息。 –

+0

没关系...我在发布.xml文件后发现了问题,我应该使用R.id.checkBox代替R.id.checkbox – HextechEnthusiast

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

     } 
    }); 
相关问题