1

I have this code in the main activity:

package flash.light.pro;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.graphics.Color;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.view.View.OnLongClickListener;


public class MainActivity extends Activity {
    private boolean isBlack = false;
    private boolean isLedOn = false;
    private boolean supportsCamera = true;
    private Camera camera;
    final Parameters p = camera.getParameters();
    protected void onStop() {
        super.onStop();

        if (camera != null) {
            camera.release();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        final Button screen = (Button) findViewById (R.id.screen);
        Context context = this;
        PackageManager pm = context.getPackageManager();

        isBlack = false;
        setBrightness(255);

        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Log.e("err", "Device has no camera!");
            supportsCamera = false;
        }


        try{
            camera = Camera.open();
        } catch (Exception e){
            Log.e(getString(R.string.app_name), "failed to open Camera");
            e.printStackTrace();
        }

        screen.setOnClickListener(new Button.OnClickListener(){  
            public void onClick(View v) { 
                if (isBlack){ 
                    screen.setBackgroundColor(Color.WHITE);
                    isBlack = false;
                    setBrightness(255);
                }
                else{
                    screen.setBackgroundColor(Color.BLACK);
                    isBlack = true;
                    setBrightness(0);
                }
            }
        });

        if(supportsCamera){
            screen.setOnLongClickListener(new OnLongClickListener() {
                public boolean onLongClick(View v) {
                    if(isLedOn){
                        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                        camera.setParameters(p);
                        camera.startPreview();
                        return true;
                    }
                    else{
                        p.setFlashMode(Parameters.FLASH_MODE_OFF);
                        camera.setParameters(p);
                        camera.stopPreview();
                        return true;
                    }
                }
            });
        }
    }

    private void setBrightness(int brightness) {
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.screenBrightness = brightness / 100.0f;
        getWindow().setAttributes(layoutParams);
    }
}

and this androidManifest.xml code:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="flash.light.pro"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
     <permission android:name="android.permission.FLASHLIGHT"
             android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
             android:protectionLevel="normal"
             android:label="string/permlab_flashlight"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

this is the LogCat logs:

11-14 20:05:06.772: D/AndroidRuntime(380): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-14 20:05:06.772: D/AndroidRuntime(380): CheckJNI is ON
11-14 20:05:07.371: D/AndroidRuntime(380): Calling main entry com.android.commands.pm.Pm
11-14 20:05:07.392: D/AndroidRuntime(380): Shutting down VM
11-14 20:05:07.400: I/AndroidRuntime(380): NOTE: attach of thread 'Binder Thread #3' failed
11-14 20:05:07.411: D/dalvikvm(380): GC_CONCURRENT freed 101K, 71% free 297K/1024K, external 0K/0K, paused 1ms+1ms
11-14 20:05:07.411: D/dalvikvm(380): Debugger has detached; object registry had 1 entries
11-14 20:05:07.830: D/AndroidRuntime(390): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-14 20:05:07.830: D/AndroidRuntime(390): CheckJNI is ON
11-14 20:05:08.390: D/AndroidRuntime(390): Calling main entry com.android.commands.am.Am
11-14 20:05:08.420: I/ActivityManager(61): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] 

flg=0x10000000 cmp=flash.light.pro/.MainActivity } from pid 390
11-14 20:05:08.450: I/ActivityManager(61): Start proc flash.light.pro for activity flash.light.pro/.MainActivity: pid=398 uid=10040 gids={1006}
11-14 20:05:08.480: D/AndroidRuntime(390): Shutting down VM
11-14 20:05:08.490: D/dalvikvm(390): GC_CONCURRENT freed 102K, 69% free 319K/1024K, external 0K/0K, paused 1ms+3ms
11-14 20:05:08.500: D/dalvikvm(390): Debugger has detached; object registry had 1 entries
11-14 20:05:09.270: D/AndroidRuntime(398): Shutting down VM
11-14 20:05:09.270: W/dalvikvm(398): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-14 20:05:09.330: E/AndroidRuntime(398): FATAL EXCEPTION: main
11-14 20:05:09.330: E/AndroidRuntime(398): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

{flash.light.pro/flash.light.pro.MainActivity}: java.lang.NullPointerException
11-14 20:05:09.330: E/AndroidRuntime(398):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
11-14 20:05:09.330: E/AndroidRuntime(398):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-14 20:05:09.330: E/AndroidRuntime(398):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-14 20:05:09.330: E/AndroidRuntime(398):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-14 20:05:09.330: E/AndroidRuntime(398):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 20:05:09.330: E/AndroidRuntime(398):  at android.os.Looper.loop(Looper.java:123)
11-14 20:05:09.330: E/AndroidRuntime(398):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-14 20:05:09.330: E/AndroidRuntime(398):  at java.lang.reflect.Method.invokeNative(Native Method)
11-14 20:05:09.330: E/AndroidRuntime(398):  at java.lang.reflect.Method.invoke(Method.java:507)
11-14 20:05:09.330: E/AndroidRuntime(398):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-14 20:05:09.330: E/AndroidRuntime(398):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-14 20:05:09.330: E/AndroidRuntime(398):  at dalvik.system.NativeStart.main(Native Method)
11-14 20:05:09.330: E/AndroidRuntime(398): Caused by: java.lang.NullPointerException
11-14 20:05:09.330: E/AndroidRuntime(398):  at flash.light.pro.MainActivity.<init>(MainActivity.java:22)
11-14 20:05:09.330: E/AndroidRuntime(398):  at java.lang.Class.newInstanceImpl(Native Method)
11-14 20:05:09.330: E/AndroidRuntime(398):  at java.lang.Class.newInstance(Class.java:1409)
11-14 20:05:09.330: E/AndroidRuntime(398):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-14 20:05:09.330: E/AndroidRuntime(398):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
11-14 20:05:09.330: E/AndroidRuntime(398):  ... 11 more
11-14 20:05:09.410: W/ActivityManager(61):   Force finishing activity flash.light.pro/.MainActivity
11-14 20:05:09.940: W/ActivityManager(61): Activity pause timeout for HistoryRecord{4060c450 flash.light.pro/.MainActivity}
11-14 20:05:20.739: W/ActivityManager(61): Activity destroy timeout for HistoryRecord{4060c450 flash.light.pro/.MainActivity}
11-14 20:06:00.031: I/dalvikvm(61): Jit: resizing JitTable from 1024 to 2048
11-14 20:06:19.377: D/SntpClient(61): request time failed: java.net.SocketException: Address family not supported by protocol
11-14 20:08:35.071: I/Process(398): Sending signal. PID: 398 SIG: 9
11-14 20:08:35.081: I/ActivityManager(61): Process flash.light.pro (pid 398) has died.
11-14 20:08:35.111: W/InputManagerService(61): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub

$Proxy@4051a740

The app crashes on start up, no errors and the log is not clear for me. What can I do to fix it. By the why the app should be some sort of FlashLight. Thanks for the help.

vlio20
  • 8,955
  • 18
  • 95
  • 180

2 Answers2

10
private Camera camera;
final Parameters p = camera.getParameters();

camera is not yet initialized

I was able to find that out by looking at your 22nd line as the logcat output indicates. Don't be overwhelmed by the amount of information in the logcat, usually finding the first instance of your package name will indicate what/where the problem is.

Edit: as a sidenote, your package name should be in reverse domain order. So for instance if your app is called flashlightpro and your domain name is example.com, your package name should be com.example.flashlightpro. It's not required, but it's a standard convention.

Edit 2: Change the declaration of camera to: Camera camera = Camera.open();

Edit 3: The Android documentation deals with taking a picture, but steps 1, 2, 3, 10 are important in your case:

Obtain an instance of Camera from open(int).
Get existing (default) settings with getParameters().
If necessary, modify the returned Camera.Parameters object and call setParameters(Camera.Parameters).
...
Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open()it in onResume()).

http://developer.android.com/reference/android/hardware/Camera.html

ajacian81
  • 7,419
  • 9
  • 51
  • 64
2

You are trying to access something which is not initiliazed and that's why getting Null Pointer Exception.The log says it clearly.

Check your Activity 22nd line you are trying to access the camera which is not even initiliazed yet.

Caused by: java.lang.NullPointerException
11-14 20:05:09.330: E/AndroidRuntime(398):  at flash.light.pro.MainActivity.<init>    (MainActivity.java:22)
Serdar Dogruyol
  • 5,147
  • 3
  • 24
  • 32