-3

I am very new to coding in Android. Trying to have my code listen for a click on an image button and I get a null pointer exception 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();
            }
        }
    });

I am sure I referenced the button in my fragment xml and the id matches the one on the code. I was wondering if I could get some help

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

Thanks a ton!

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • So the view is not in the layout you set with `setContentView()` but in some fragment layout? http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate – laalto Aug 12 '14 at 21:00

1 Answers1

0

I don't see a declaration for btnswitch

replace

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

with

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

Or if you have declared btnswitch but not included that part in code here try checking the functioning on switch and remove flash functions and add a toast alert to check button

Replace

 btnSwitch.setOnClickListener(new View.OnClickListener() {

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

with

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

             }
    }
});

This will help you check if the problem is in click listener or turnOnFlash() function.

Akshit Rewari
  • 941
  • 2
  • 13
  • 26