开启手电筒时应用程序崩溃

时间:2015-11-02 19:14:00

标签: android galaxy led flashlight

有人! 我正在尝试为Android制作手电筒应用程序。当我按下按钮上的手电筒时,应用程序崩溃了。我已经尝试了很多网站,在Stack Overflow中看到了他们的解决方案和许多Q,但它们都没有为我工作! 这是我的应用程序代码:

爪哇:

    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)和许多其他设备上进行了测试,它在所有设备上都崩溃了他们!

提前致谢!

1 个答案:

答案 0 :(得分:2)

  

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;
}}