Android:无法获取相机实例

时间:2013-01-31 08:10:20

标签: android

我有一个简单的测试类

公共类MainActivity扩展了Activity {

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

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    Camera a = Camera.open();
}

}

和清单文件

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.cameratest.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>
</application>

应用程序运行后我有异常

01-31 10:05:46.033: E/AndroidRuntime(17154): FATAL EXCEPTION: main
01-31 10:05:46.033: E/AndroidRuntime(17154): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cameratest/com.example.cameratest.MainActivity}: java.lang.RuntimeException: Fail to connect to camera service
01-31 10:05:46.033: E/AndroidRuntime(17154):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at android.os.Looper.loop(Looper.java:137)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at android.app.ActivityThread.main(ActivityThread.java:4514)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at java.lang.reflect.Method.invokeNative(Native Method)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at java.lang.reflect.Method.invoke(Method.java:511)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at dalvik.system.NativeStart.main(Native Method)
01-31 10:05:46.033: E/AndroidRuntime(17154): Caused by: java.lang.RuntimeException: Fail to connect to camera service
01-31 10:05:46.033: E/AndroidRuntime(17154):    at android.hardware.Camera.native_setup(Native Method)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at android.hardware.Camera.<init>(Camera.java:349)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at android.hardware.Camera.open(Camera.java:326)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at com.example.cameratest.MainActivity.onCreate(MainActivity.java:17)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at android.app.Activity.performCreate(Activity.java:4465)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
01-31 10:05:46.033: E/AndroidRuntime(17154):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
01-31 10:05:46.033: E/AndroidRuntime(17154):    ... 11 more

我做错了什么?我的设备肯定有一个摄像头,它可以在其他应用程序上运行......

0 个答案:

没有答案