我一直在開發一個簡單的閃光燈應用程序,它的工作原理,但只有一個問題,當我點擊開關按鈕它切換燈,但當我再次點擊應用程序崩潰...切換按鈕崩潰android應用程序
package com.example.flashapp;
import android.hardware.Camera;
import android.os.Bundle;
import android.app.*;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
ToggleButton toggleButton;
Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = (ToggleButton)findViewById(R.id.onOffFlashlight);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton,boolean checked) {
//ToDo something
camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
Toast.makeText(getApplicationContext(), "Flash Light is ON", Toast.LENGTH_LONG).show();
}
});
}
}
可能的話,你需要再次啓動 –