2

我正在使用此代码将相机 LED 用作手电筒,但在服务启动时它会崩溃。使用 Log.w() 语句,我发现它在 param = camera.getParameters(); 处停止。我已将 android.permission.CAMERA 包含在我的清单中。我不明白是什么导致了崩溃。我怎样才能解决这个问题?

    public class Flashlight extends Service {

    private Camera camera;
    private Parameters param;
    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder {
        Flashlight getService() {
            return Flashlight.this;
        }
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
         Log.w("1","camera.open");
         Camera.open();
         Log.w("1","camera.getParameters");
         param = camera.getParameters();
         Log.w("1","i got camera parameters");
         List<String> pList = camera.getParameters().getSupportedFlashModes();
         if(pList.contains(Parameters.FLASH_MODE_TORCH)){
             param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
         }
         else if(pList.contains(Parameters.FLASH_MODE_ON)){
             param.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
         }
         param.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
         try{
             camera.setParameters(param);
             camera.startPreview();
         }
         catch (Exception e){
             Toast.makeText(getApplicationContext(), "Your device does not have flash light support", Toast.LENGTH_SHORT).show();

         }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Parameters p=camera.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(p);
        camera.stopPreview();
        camera.release();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}

这是 LogCcat

12-15 15:44:32.037: W/1(26304): camera.open
12-15 15:44:32.347: W/1(26304): camera.getParameters
12-15 15:44:32.347: D/AndroidRuntime(26304): Shutting down VM
12-15 15:44:32.347: W/dalvikvm(26304): threadid=1: thread exiting with uncaught exception (group=0x40a9e228)
12-15 15:44:32.357: E/AndroidRuntime(26304): FATAL EXCEPTION: main
12-15 15:44:32.357: E/AndroidRuntime(26304): java.lang.RuntimeException: Unable to start service com.bill.deuterh.Flashlight@40dd0978 with Intent { cmp=com.bill.deuterh/.Flashlight }: java.lang.NullPointerException
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2738)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.app.ActivityThread.access$1900(ActivityThread.java:139)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1334)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.os.Looper.loop(Looper.java:154)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.app.ActivityThread.main(ActivityThread.java:4945)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at java.lang.reflect.Method.invokeNative(Native Method)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at java.lang.reflect.Method.invoke(Method.java:511)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at dalvik.system.NativeStart.main(Native Method)
12-15 15:44:32.357: E/AndroidRuntime(26304): Caused by: java.lang.NullPointerException
12-15 15:44:32.357: E/AndroidRuntime(26304):    at com.bill.deuterh.Flashlight.onStartCommand(Flashlight.java:32)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2715)
12-15 15:44:32.357: E/AndroidRuntime(26304):    ... 10 more

经过大量试用结束错误后,我终于弄明白了。我正在为其他有此问题的用户发布获取参数的正确代码

     camera = Camera.open();
     Camera.Parameters param = camera.getParameters();
4

2 回答 2

3

首先添加一个条件来检查对象是否为空:

camera = Camera.open();
if (camera.getParameters()!=null) {
    Camera.Parameters param = camera.getParameters();
}
于 2014-09-05T06:44:35.413 回答
0

您的相机对象在 处为空camera.getParameters()。调用Camera.open()以使用它。

于 2013-12-15T12:11:46.820 回答