-Android主页按钮崩溃应用程序

时间:2014-01-24 07:15:01

标签: android crash wakelock flashlight

我正在举一个手电筒的例子,但我发现了两个问题: 1.- Home被迫应用程序崩溃 2.-当屏幕关闭时手电筒关闭 我试图使用唤醒锁但不起作用

代码:

@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)
有人能帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:1)

尝试关注surfaceDestroyed中的代码:

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

并从onStop()方法移除 cam.release(); ,因为您在onPause()中释放了相机,然后您不需要在{{1}中再次执行此操作}}