点击时相机手电筒项目已停止

时间:2013-01-22 16:51:28

标签: android

我正在使用相机手电筒项目。

它在模拟器上加载但是当我点击“开”按钮时它意外停止。这是cat-log https://dl.dropbox.com/u/15065300/logcat1.png

以下是代码:

public class FlashLight extends Activity {


    private final static String LOG_TAG = "FlashLight";  
    private Button mOnBtn;   
    private Camera mCamera;    
    private boolean isActive;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

      //this will be inside your onCreate...
        mOnBtn = (Button) findViewById(R.id.on_btn);
        mOnBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                flipSwitch();
                processClick();

            }
        });
   }

    //these will be outside your onCreate
    public void flipSwitch() {
        isActive = !isActive;
    }

    @Override
    protected void onResume() {
        super.onResume();
        try{
            mCamera = Camera.open();
            mCamera.startPreview();
            Toast.makeText(getApplicationContext(),"Camera is  present", Toast.LENGTH_LONG).show();

        } catch( Exception e ){
            Log.e(LOG_TAG, "Impossible d'ouvrir la camera");
        }
    }

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

    public void processClick() {
        if(isActive) {
            Parameters params = mCamera.getParameters();
            params.set("flash-mode", "torch");
            mCamera.setParameters( params );
            mCamera.startPreview();
        }

        else { 

             Parameters params = mCamera.getParameters();
             params.set("flash-mode", "off");
             mCamera.setParameters( params );
             mCamera.stopPreview();

        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码中NullPointerException第74行有Flashlight.java

可能mCameraparams正在被使用,而它的值为null。检查这些值的初始化/分配。