我试图在单击按钮时显示进度条。当我测试应用程序时,它会强制关闭/停止。在添加进度条代码之前,我的应用程序运行良好。
如果这很重要,我也正在使用切换按钮,并且我希望旋转进度条不是对话框。
有什么帮助吗?
这是我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonFlashlight);
Context context = this;
PackageManager pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (isLighOn) {
progressBar1.setVisibility(View.INVISIBLE);
final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.drawable.click);
mp1.start();
Log.i("info", "torch is turn off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 1.0f;
getWindow().setAttributes(params);
} else {
progressBar1.setVisibility(View.VISIBLE);
final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.drawable.click);
mp1.start();
Log.i("info", "torch is turn on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 0.05f;
getWindow().setAttributes(params);
}
}
});
}