不幸的是,在模拟器中运行时应用程序已停止

时间:2012-08-25 18:49:14

标签: android eclipse nullpointerexception

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

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

2 个答案:

答案 0 :(得分:1)

由于您的AVD没有拿到相机,我认为这可能对您有所帮助: Enable Camera in AVD

答案 1 :(得分:0)

您的错误跟踪显示您Activity的第44行发生了异常。通过计算您发布的代码行,这似乎是Camera.open()。根据文档,如果设备没有后置摄像头,则返回null。如果它在模拟器上,这可能是有意义的。