我最终想把这个程序变成一个可调频率的闪光灯。但现在我只是试图解决基本问题。每次我使用parseInt时,应用程序都会崩溃。在这段代码中,我在strobe()方法中使用它,但我也尝试在其他地方使用它。我还尝试使用它来创建一个变量。它们最终都导致了同样的结果(应用程序崩溃)。有人能解释一下为什么会发生这种情况吗?
EditText box1, box2;
Button toggle;
int firstNum;
String string1;
Camera cam;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
makeVariables();
toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
strobe();
}
});
}
private void makeVariables(){
box1 = (EditText)findViewById(R.id.editText1);
box2 = (EditText)findViewById(R.id.editText2);
string1 = box1.toString();
string2 = box2.toString();
toggle = (Button)findViewById(R.id.button1);
}
private void turnOnLight(){
cam = Camera.open();
Parameters params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(params);
cam.startPreview();
cam.autoFocus(new AutoFocusCallback(){
public void onAutoFocus(boolean success, Camera camera) {
}
});
}
private void turnOffLight(){
cam.stopPreview();
cam.release();
}
private void strobe(){
Thread timer = new Thread(){
public void run(){
turnOnLight();
try{
sleep(Integer.ParseInt(box1.toString()));
}catch(InterruptedException e){
e.printStackTrace();
}finally{
turnOffLight();
}
}
};
timer.start();
}
}