0

我是 android devlopment 的绝对初学者,我想制作一个相机开/关应用程序,但我得到了错误。所以请帮助我。

package com.vikram.flashcamera;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private boolean isLighOn = false;
    private Camera camera;
    private Button button;

    @Override
    protected void onStop() {
        super.onStop();

        if(camera != null) {
            camera.release();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.buttonClick);

        Context context = this;
        PackageManager pm = context.getPackageManager();

        if(!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Log.e("err", "Device has no camera!");
            return;
        }

        camera = Camera.open();
        final Parameters p = camera.getParameters();

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if(isLighOn) {
                    Log.i("info", "torch is turn off!");
                    p.setFlashMode(Parameters.FLASH_MODE_OFF);
                    camera.setParameters(p);
                    camera.stopPreview();
                    isLighOn = false;
                }
                else {
                   Log.i("info", "torch is turn on!");
                    p.setFlashMode(Parameters.FLASH_MODE_TORCH);

                    camera.setParameters(p);
                    camera.startPreview();
                    isLighOn = true;
                }
            }
        });
    }
}

下面给出了我的 logCat 错误。

    08-25 15:50:41.932: E/Trace(878): error opening trace file: No such file or directory (2)
    08- 5 15:50:42.453: D/AndroidRuntime(878): Shutting down VM
    08-25 15:50:42.463: W/dalvikvm(878): threadid=1: thread exiting with uncaught exception     (group=0x40a13300)
    08-25 15:50:42.473: E/AndroidRuntime(878): FATAL EXCEPTION: main
    08-25 15:50:42.473: E/AndroidRuntime(878): java.lang.RuntimeException: Unable to start     activity         ComponentInfo{com.vikram.flashcamera/com.vikram.flashcamera.MainActivity}:     java.lang.NullPointerException
    08-25 15:50:42.473: E/AndroidRuntime(878):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    08-25 15:50:42.473: E/AndroidRuntime(878):  at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-25 15:50:42.473: E/AndroidRuntime(878):  at         android.app.ActivityThread.access$600(ActivityThread.java:130)
08-25 15:50:42.473: E/AndroidRuntime(878):  at         android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    08-25 15:50:42.473: E/AndroidRuntime(878):  at android.os.Handler.dispatchMessage(Handler.java:99)
    08-25 15:50:42.473: E/AndroidRuntime(878):  at android.os.Looper.loop(Looper.java:137)
    08-25 15:50:42.473: E/AndroidRuntime(878):  at android.app.ActivityThread.main(ActivityThread.java:4745)
    08-25 15:50:42.473: E/AndroidRuntime(878):  at java.lang.reflect.Method.invokeNative(Native Method)
    08-25 15:50:42.473: E/AndroidRuntime(878):  at java.lang.reflect.Method.invoke(Method.java:511)
    08-25 15:50:42.473: E/AndroidRuntime(878):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    08-25 15:50:42.473: E/AndroidRuntime(878):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    08-25 15:50:42.473: E/AndroidRuntime(878):  at dalvik.system.NativeStart.main(Native Method)
    08-25 15:50:42.473: E/AndroidRuntime(878): Caused by: java.lang.NullPointerException
    08-25 15:50:42.473: E/AndroidRuntime(878):  at com.vikram.flashcamera.MainActivity.onCreate(MainActivity.java:44)
    08-25 15:50:42.473: E/AndroidRuntime(878):  at android.app.Activity.performCreate(Activity.java:5008)
    08-25 15:50:42.473: E/AndroidRuntime(878):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    08-25 15:50:42.473: E/AndroidRuntime(878):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
    08-25 15:50:42.473: E/AndroidRuntime(878):  ... 11 more
    08-25 18:03:08.543: E/Trace(900): error opening trace file: No such file or directory (2)
    08-25 18:03:09.122: D/AndroidRuntime(900): Shutting down VM
    08-25 18:03:09.122: W/dalvikvm(900): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
    08-25 18:03:09.132: E/AndroidRuntime(900): FATAL EXCEPTION: main
    08-25 18:03:09.132: E/AndroidRuntime(900): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vikram.flashcamera/com.vikram.flashcamera.MainActivity}:     java.lang.NullPointerException
    08-25 18:03:09.132: E/AndroidRuntime(900):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at android.os.Handler.dispatchMessage(Handler.java:99)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at android.os.Looper.loop(Looper.java:137)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at android.app.ActivityThread.main(ActivityThread.java:4745)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at java.lang.reflect.Method.invokeNative(Native Method)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at java.lang.reflect.Method.invoke(Method.java:511)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at dalvik.system.NativeStart.main(Native Method)
    08-25 18:03:09.132: E/AndroidRuntime(900): Caused by: java.lang.NullPointerException
    08-25 18:03:09.132: E/AndroidRuntime(900):  at com.vikram.flashcamera.MainActivity.onCreate(MainActivity.java:44)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at android.app.Activity.performCreate(Activity.java:5008)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    08-25 18:03:09.132: E/AndroidRuntime(900):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
    08-25 18:03:09.132: E/AndroidRuntime(900):  ... 11 more
    08-25 18:03:11.613: I/Process(900): Sending signal. PID: 900 SIG: 9
    08-25 18:33:52.222: E/Trace(929): error opening trace file: No such file or directory    (2)
    08-25 18:33:52.813: D/AndroidRuntime(929): Shutting down VM
    08-25 18:33:52.813: W/dalvikvm(929): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
    08-25 18:33:52.833: E/AndroidRuntime(929): FATAL EXCEPTION: main
    08-25 18:33:52.833: E/AndroidRuntime(929): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vikram.flashcamera/com.vikram.flashcamera.MainActivity}: java.lang.NullPointerException
    08-25 18:33:52.833: E/AndroidRuntime(929):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at android.os.Handler.dispatchMessage(Handler.java:99)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at android.os.Looper.loop(Looper.java:137)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at android.app.ActivityThread.main(ActivityThread.java:4745)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at java.lang.reflect.Method.invokeNative(Native Method)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at java.lang.reflect.Method.invoke(Method.java:511)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at dalvik.system.NativeStart.main(Native Method)
    08-25 18:33:52.833: E/AndroidRuntime(929): Caused by: java.lang.NullPointerException
    08-25 18:33:52.833: E/AndroidRuntime(929):  at com.vikram.flashcamera.MainActivity.onCreate(MainActivity.java:44)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at android.app.Activity.performCreate(Activity.java:5008)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    08-25 18:33:52.833: E/AndroidRuntime(929):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
    08-25 18:33:52.833: E/AndroidRuntime(929):  ... 11 more
    08-25 18:38:52.904: I/Process(929): Sending signal. PID: 929 SIG: 9
4

2 回答 2

1

由于您的 AVD 没有得到相机,我认为这可能会对您有所帮助: 在 AVD 中启用相机

于 2012-08-26T00:54:38.507 回答
0

您的错误跟踪显示异常发生在您的Activity. 从您发布的代码行数来看,这似乎在Camera.open(). 根据文档,null如果设备没有后置摄像头,则会返回。如果它在模拟器上,这可能是有道理的。

于 2012-08-25T19:01:53.787 回答