0

我尝试按照本教程进行操作:http: //developer.android.com/guide/topics/media/camera.html#custom-camera

但我被困在 NullPointerException 上。

我将代码粘贴到 pastebin,所以看起来更好: http: //pastebin.com/315dUhvR

LogCat 说它在第 31 行崩溃:

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

    // Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview); // line 31 - crashes here
}

如您所见,这是日志:

01-15 23:02:33.951: E/AndroidRuntime(1124): FATAL EXCEPTION: main
01-15 23:02:33.951: E/AndroidRuntime(1124): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.provescamera/com.example.provescamera.CameraActivity}: java.lang.NullPointerException
01-15 23:02:33.951: E/AndroidRuntime(1124):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at android.os.Looper.loop(Looper.java:137)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at java.lang.reflect.Method.invoke(Method.java:511)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at dalvik.system.NativeStart.main(Native Method)
01-15 23:02:33.951: E/AndroidRuntime(1124): Caused by: java.lang.NullPointerException
01-15 23:02:33.951: E/AndroidRuntime(1124):     at com.example.provescamera.CameraActivity.onCreate(CameraActivity.java:31)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at android.app.Activity.performCreate(Activity.java:5104)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-15 23:02:33.951: E/AndroidRuntime(1124):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-15 23:02:33.951: E/AndroidRuntime(1124):     ... 11 more

我真的不明白,因为我只是复制了这段代码......我做错了什么?

哦,差点忘了。

显现:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.provescamera"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-feature android:name="android.hardware.camera" android:required="false" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.provescamera.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    <activity android:name=".CameraActivity"
          android:label="@string/app_name"

          android:screenOrientation="portrait">
          <!-- configure this activity to use landscape orientation -->

          <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    </application>

</manifest>

和基本布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />

  <Button
    android:id="@+id/button_capture"
    android:text="Capture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    />
</LinearLayout>

编辑:

它现在崩溃了第 75 行:

mCamera.setPreviewDisplay(holder);
4

3 回答 3

3

看起来您使用了错误的布局,请尝试:

setContentView(R.layout.basic_layout);
于 2013-01-15T23:16:01.860 回答
1

该死。我的第二次编辑,问题是我没有设置所有权限:

<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

错过了最后一个。

于 2013-01-17T21:33:02.210 回答
0

您尝试添加相机权限

<uses-permission android:name="android.permission.CAMERA" />

而且我认为您应该在这一行中删除 android:required="false"

<uses-feature android:name="android.hardware.camera" android:required="false" />

如果您希望此应用在没有摄像头的手机中运行,则应在没有摄像头的情况下处理异常。

于 2013-01-16T06:52:44.660 回答