试图听取Onclick获取空指针异常

时间:2014-08-12 19:34:45

标签: android onclicklistener

我对Android编码很新。试图让我的代码听取图像按钮上的单击,我得到一个空指针异常btnSwitch.setOnClickListener

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    //first determine if the phone has flash

    setContentView(R.layout.activity_flash_light);
    hasFlash = getApplicationContext().getPackageManager()
            .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

    //Turn on the camera 
    getCamera();
    //make the button
    btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);
    //Listen for click
    btnSwitch.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (isFlashOn) {
                // turn off flash
                turnOffFlash();
            } else {
                // turn on flash
                turnOnFlash();
            }
        }
    });

我确信我在我的片段xml中引用了该按钮,并且id与代码中的id匹配。我想知道我是否能得到一些帮助

logcat的

08-12 13:27:25.125: E/AndroidRuntime(28968): FATAL EXCEPTION: main
08-12 13:27:25.125: E/AndroidRuntime(28968): Process: com.connerchyung.simplestrobe, PID: 28968
08-12 13:27:25.125: E/AndroidRuntime(28968): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.connerchyung.simplestrobe/com.connerchyung.simplestrobe.FlashLight}: java.lang.NullPointerException
08-12 13:27:25.125: E/AndroidRuntime(28968):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at android.os.Looper.loop(Looper.java:136)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at android.app.ActivityThread.main(ActivityThread.java:5139)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at java.lang.reflect.Method.invokeNative(Native Method)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at java.lang.reflect.Method.invoke(Method.java:515)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at dalvik.system.NativeStart.main(Native Method)
08-12 13:27:25.125: E/AndroidRuntime(28968): Caused by: java.lang.NullPointerException
08-12 13:27:25.125: E/AndroidRuntime(28968):    at com.connerchyung.simplestrobe.FlashLight.onCreate(FlashLight.java:92)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at android.app.Activity.performCreate(Activity.java:5231)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-12 13:27:25.125: E/AndroidRuntime(28968):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
08-12 13:27:25.125: E/AndroidRuntime(28968):    ... 11 more

非常感谢!

1 个答案:

答案 0 :(得分:0)

我没有看到btnswitch的声明

替换

btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);

ImageButton btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);.

或者,如果您已声明btnswitch但未在此代码中包含该部分,请尝试检查开关上的功能并删除闪存功能并添加Toast警报以检查按钮

替换

 btnSwitch.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (isFlashOn) {
            // turn off flash
            turnOffFlash();
        } else {
            // turn on flash
            turnOnFlash();
        }
    }
});

btnSwitch.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (isFlashOn) {
Toast toast = Toast.makeText(getApplicationContext(),"ON", Toast.LENGTH_LONG).show();
            //turnOffFlash();


} 
else {

 Toast toast = Toast.makeText(getApplicationContext(),"OFF", Toast.LENGTH_LONG).show();
           // turnOnFlash();

             }
    }
});

这将帮助您检查问题是否在click listener或turnOnFlash()函数中。