打开手电筒时应用程序崩溃
App is Crashing When Turning on Flashlight
伙计们!
我正在尝试为 android 制作手电筒应用程序。当我按下手电筒按钮时,应用程序崩溃了。我尝试了很多网站,在 Stack Overflow 中看到了他们的解决方案和许多问题,其中 none 对我有用!
这是我的应用程序代码:
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) 和许多其他设备上进行了测试,它在所有设备上崩溃其中!
提前致谢!
FlashlightActivity.java
public class FlashlightActivity extends Activity {
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) {
if (Flash.getTorch()) {
flashlight_switch.setImageResource(R.drawable.flashlight_switch_on);
} else {
flashlight_switch.setImageResource(R.drawable.flashlight_switch_off);
}
}
});
}}
Flash.java
class Flash {
private static boolean flashOnOff = false;
public static Camera camera;
private static Camera.Parameters params;
public static boolean getTorch() {
if (flashOnOff) // turn off flash
off();
else // turn on flash
on();
return flashOnOff;
}
private static void on() {
if (!flashOnOff) {
if (camera == null || params == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
int a = 10;
}
}
try {
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.setPreviewTexture(new SurfaceTexture(0));
camera.startPreview();
flashOnOff = true;
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static void off() {
if (camera == null || params == null)
return;
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
flashOnOff = false;
}}
伙计们! 我正在尝试为 android 制作手电筒应用程序。当我按下手电筒按钮时,应用程序崩溃了。我尝试了很多网站,在 Stack Overflow 中看到了他们的解决方案和许多问题,其中 none 对我有用! 这是我的应用程序代码:
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) 和许多其他设备上进行了测试,它在所有设备上崩溃其中!
提前致谢!
FlashlightActivity.java
public class FlashlightActivity extends Activity {
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) {
if (Flash.getTorch()) {
flashlight_switch.setImageResource(R.drawable.flashlight_switch_on);
} else {
flashlight_switch.setImageResource(R.drawable.flashlight_switch_off);
}
}
});
}}
Flash.java
class Flash {
private static boolean flashOnOff = false;
public static Camera camera;
private static Camera.Parameters params;
public static boolean getTorch() {
if (flashOnOff) // turn off flash
off();
else // turn on flash
on();
return flashOnOff;
}
private static void on() {
if (!flashOnOff) {
if (camera == null || params == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
int a = 10;
}
}
try {
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.setPreviewTexture(new SurfaceTexture(0));
camera.startPreview();
flashOnOff = true;
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static void off() {
if (camera == null || params == null)
return;
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
flashOnOff = false;
}}