2013-12-15 54 views
1

我使用此代碼來使用相機LED作爲手電筒,但它在服務啓動時崩潰。使用Log.w()語句,我發現它停在param = camera.getParameters();.我已經在我的清單中包含了android.permission.CAMERA。我不明白是什麼導致了這次事故。我怎樣才能解決這個問題?android:camera.getParameters()崩潰應用程序

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 [email protected] 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 

我終於有這個問題

想通了大量的試驗結束error.i'm後發佈正確的代碼用於獲取parameteres,爲其他用戶
 camera = Camera.open(); 
    Camera.Parameters param = camera.getParameters(); 

回答

0

您的相機對象爲空camera.getParameters()。致電Camera.open()爲了使用它。

+0

我已經試過了。我的錯誤發佈代碼沒有it.i現在就更新它。我也試過Camera.open(0);正如我在另一篇文章中看到的,但它仍然崩潰。 – Anonymous

+0

你可以添加日誌形式logcat嗎? – Hellboy

+0

ofcourse。我已經做了 – Anonymous

2

添加一個條件,以檢查對象爲null第一:

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