2014-01-24 214 views
1

我做手電筒的一個例子,但我發現了兩個問題: 1 .-首頁按下應用程序崩潰 2:屏幕關閉時,閃光燈關閉 我曾嘗試使用喚醒鎖,但不工作-Android home鍵崩潰的應用程序

代碼:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    Log.d(TAG, "onCreate()"); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    context = MainActivity.this; 
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){ 
     mTorch = (ToggleButton) findViewById(R.id.toggleButton1); 
     mTorch.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       Log.d(TAG, "onCheckedChanged"); 
       try{ 
        if (cam == null){ 
         cam = Camera.open(); 
        } 
        camParams = cam.getParameters(); 
        List<String> flashModes = camParams.getSupportedFlashModes(); 
        if (isChecked){ 
         if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) { 
          camParams.setFlashMode(Parameters.FLASH_MODE_TORCH); 
         }else{ 
          showDialog(MainActivity.this, FLASH_TORCH_NOT_SUPPORTED); 
         } 
        } else { 
         camParams.setFlashMode(Parameters.FLASH_MODE_OFF); 
        } 
        cam.setParameters(camParams); 
        cam.startPreview(); 
       }catch (Exception e) { 
        Log.d(TAG, "Caught " + e); 
        Toast.makeText(MainActivity.this, 
         "Camera/Torch failure: " + e, Toast.LENGTH_SHORT).show(); 
        e.printStackTrace(); 
        if (cam != null) { 
         cam.stopPreview(); 
         cam.release(); 
        } 
       } 
      } 
     }); 
     surfaceView = (SurfaceView) this.findViewById(R.id.hiddenSurfaceView); 
     surfaceHolder = surfaceView.getHolder(); 
     surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
     surfaceHolder.addCallback(this); 
    } else { 
     showDialog(MainActivity.this, FLASH_NOT_SUPPORTED); 
    } 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if(cam == null){ 
     cam = Camera.open(); 
    } 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    cam.release(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if(cam != null){ 
     cam.release(); 
    } 
} 

public void showDialog (Context context, int dialogId) { 
    switch(dialogId){ 
    case FLASH_NOT_SUPPORTED: 
     builder = new AlertDialog.Builder(context); 
     builder.setMessage("Sorry, Your phone does not support Camera Flash") 
     .setCancelable(false) 
     .setNeutralButton("Close", new OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       finish(); 
      } 
     }); 
     alertDialog = builder.create(); 
     alertDialog.show(); 
     break; 
    case FLASH_TORCH_NOT_SUPPORTED: 
     builder = new AlertDialog.Builder(context); 
     builder.setMessage("Sorry, Your camera flash does not support torch feature") 
     .setCancelable(false) 
     .setNeutralButton("Close", new OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       finish(); 
      } 
     }); 
     alertDialog = builder.create(); 
     alertDialog.show(); 
    } 

} 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
    // Empty - required by interface 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    try { 
     cam.setPreviewDisplay(holder); 
    } catch (IOException e) { 
     Log.e(TAG, "Unexpected IO Exception in setPreviewDisplay()", e); 
    } 
} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
    // Empty - required by interface. 
} 


} 

,這是logcat的錯誤:

01-24 01:06:15.546: E/AndroidRuntime(25680): FATAL EXCEPTION: main 
01-24 01:06:15.546: E/AndroidRuntime(25680): java.lang.RuntimeException: Method called after release() 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.hardware.Camera.setPreviewDisplay(Native Method) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.hardware.Camera.setPreviewDisplay(Camera.java:460) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at com.simpleflashlightdemo.MainActivity.surfaceCreated(MainActivity.java:152) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.view.SurfaceView.updateWindow(SurfaceView.java:533) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:226) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.view.View.dispatchWindowVisibilityChanged(View.java:5854) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1007) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2558) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.os.Looper.loop(Looper.java:137) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at android.app.ActivityThread.main(ActivityThread.java:4697) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at java.lang.reflect.Method.invokeNative(Native Method) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at java.lang.reflect.Method.invoke(Method.java:511) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 
01-24 01:06:15.546: E/AndroidRuntime(25680): at dalvik.system.NativeStart.main(Native Method) 

有人可以幫助我嗎?謝謝!在surfaceDestroyed

+0

通過看行: '一月一日至24日:06:15.546:E/AndroidRuntime(25680):在com.simpleflashlightdemo.MainActivity.surfaceCreated(MainActivity.java:152)' 有在生產線的問題** 'MainActivity.java'中的** ** –

回答

1

嘗試以下代碼:

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
     cam.stopPreview(); 
     cam.setPreviewCallback(null); 
     cam.release(); 
     cam = null; 
    } 

和除去cam.release();onStop()方法,因爲你釋放你的相機在onPause()那麼你不需要再在onStop()做這個。

+0

當我按下主頁按鈕時,應用程序崩潰:/ – Loreln

+0

當您按下後退按鈕時,應用程序崩潰了嗎? –

+0

是再次崩潰,當我按回按鈕 – Loreln