我尝试过这个教程: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>
和basic_layout:
<?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);
答案 0 :(得分:3)
看起来您使用了错误的布局,请尝试:
setContentView(R.layout.basic_layout);
答案 1 :(得分: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" />
错过了最后一个。
答案 2 :(得分:0)
您尝试添加相机权限
<uses-permission android:name="android.permission.CAMERA" />
我认为你应该删除这行中的android:required =“false”
<uses-feature android:name="android.hardware.camera" android:required="false" />
如果您希望此应用在没有相机的手机中工作, 如果没有摄像头,你应该处理例外情况。