我正在尝试创建一个用于在 android 上打开和关闭手电筒的应用程序,但遇到了一些我无法解决的错误。这些错误似乎只出现在 java 活动中,并且似乎与我所做的“cam”活动有关。当我悬停时,在我的 java cam.release 底部显示“添加演员表”。
感谢您在高级方面的帮助。
MainActivity.java
public class MainActivity extends Activity implements OnCheckedChangeListener {
Camera cam;
ToggleButton mTorch;
Parameters camParams;
private Context context;
AlertDialog.Builder builder;
AlertDialog alertDialog;
private final int FLASH_NOT_SUPPORTED = 0;
private final int FLASH_TORCH_NOT_SUPPORTED = 1;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = MainActivity.this;
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH)) {
mTorch = (ToggleButton) findViewById(R.id.toggleButton1);
} else {
showDialog(MainActivity.this,FLASH_NOT_SUPPORTED);
}
}
private void showDialog(MainActivity mainActivity, int fLASH_NOT_SUPPORTED2) {
// TODO Auto-generated method stub
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mTorch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@SuppressWarnings("deprecation")
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
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_NOT_SUPPORTED);
}
} else {
camParams.setFlashMode(Parameters.FLASH_MODE_OFF);
}
cam.setParameters(camParams);
cam.startPreview();
} catch (Exception e) {
e.printStackTrace();
cam.stopPreview();
cam.release();
}
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (cam == null) {
cam = Camera.open();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (cam != null) {
cam.release();
}
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
cam.release();
}
}
显现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.t.torch"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature
android:name="android.hardware.Camera"
android:required="true" >
</uses-feature>
<uses-feature
android:name="android.hardware.camera.FLASHLIGHT"
android:required="true" >
</uses-feature>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.t.torch.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dip"
android:text="@string/app_name"
android:textSize="25sp" />
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dip"
android:text="ToggleButton" >
</ToggleButton>
</LinearLayout>