【发布时间】:2023-03-18 06:26:01
【问题描述】:
伙计们! 我正在尝试为 android 制作手电筒应用程序。当我按下手电筒按钮时,应用程序崩溃。我尝试了很多网站,在 Stack Overflow 中看到了他们的解决方案和许多问题,但没有一个对我有用! 这是我的应用代码:
Java:
public class FlashlightActivity extends Activity {
private Camera cameraObj;
ImageButton flashlight_switch;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flashlight);
flashlight_switch = (ImageButton) findViewById(R.id.flashlight_switch);
flashlight_switch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Camera.Parameters cameraParams =cameraObj.getParameters();
cameraParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
cameraObj.setParameters(cameraParams);
cameraObj.startPreview();
flashlight_switch.setImageResource(R.drawable.flashlight_switch_on);
}
});
flashlight_switch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Camera.Parameters cameraParams = cameraObj.getParameters();
cameraParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
cameraObj.setParameters(cameraParams);
cameraObj.stopPreview();
flashlight_switch.setImageResource(R.drawable.flashlight_switch_off);
}
});
}
}
清单:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<permission android:name="android.permission.FLASHLIGHT" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.germinai.ledflashlight.FlashlightActivity"
android:label="@string/app_name"
android:screenOrientation="nosensor" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="@string/orientation"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.germinai.ledflashlight.MainActivity$PlaceholderFragment"
android:background="@color/grey"
>
<ImageButton
android:id="@+id/flashlight_switch"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:src="@drawable/flashlight_switch_off"
android:background="@null"
android:contentDescription="@null"
android:hapticFeedbackEnabled="true"
android:adjustViewBounds="false"
android:layout_alignWithParentIfMissing="true"
android:padding="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageButton
android:id="@+id/sos_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sos_off"
android:background="@null"
android:contentDescription="@null"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:minHeight="12dp"
android:minWidth="12dp"
android:maxWidth="500dp"
android:maxHeight="500dp" />
我尽力不寻求帮助,但现在我真的无法弄清楚。我是初学者,所以您详细解释答案会非常好。如有错别字请忽略。 该应用程序在 Galaxy S3 (4.4 kitkat) Galaxy Note 1 (4.0) P8 Lite (5.0.1) Galaxy Grand Prime (5.1.1 Lollipop) Droid Razr M (4.4 Kitkat) 和许多其他设备上进行了测试,它在所有设备上都崩溃了他们!
提前致谢!
【问题讨论】:
标签: android galaxy led flashlight