android:cCamera.getParameters()使应用程序崩溃



我使用此代码将相机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();

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

camera = Camera.open();
if (camera.getParameters()!=null) {
    Camera.Parameters param = camera.getParameters();
}

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

相关内容

  • 没有找到相关文章

最新更新