gpt4 book ai didi

java - 我的手电筒应用程序出现错误

转载 作者:行者123 更新时间:2023-12-01 15:01:29 26 4
gpt4 key购买 nike

我正在尝试创建一个用于在 Android 上打开和关闭手电筒的应用程序,但遇到了一些无法解决的错误。这些错误似乎只出现在 java Activity 中,并且似乎与我所做的“cam” Activity 有关。当我将鼠标悬停在 java cam.release 的底部时,它会显示“添加 Actor ”。

感谢您的提前帮助。

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();

}
}

list

<?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>

最佳答案

您的主要问题是 cam 从未被设置。您可能会在与该变量相关的任何行上收到 NullPointerExceptions。

原因是:

if (cam != null) {
cam = Camera.open();
}

当然应该是:

if (cam == null) {
cam = Camera.open();
}

另请注意,在调用 onStop() 期间,cam 可能为 null。

关于java - 我的手电筒应用程序出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13580556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com