我正在尝试开发Android应用程序以获得相机的数量..但每次我启动应用程序时它说:无法启动actvity ComponentInfo。 所以我必须做些什么来解决这个问题..我不知道从哪里开始! 这是我的代码:
清单:
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
</manifest>
activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
java文件:
package com.example.test;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView v = (TextView)findViewById(R.id.tv1);
//int x = checkcam();
v.setText(Camera.getNumberOfCameras());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public int checkcam()
{
int no =Camera.getNumberOfCameras();
if(no == 1)
{
return 1 ;
}
return 2;
}
}
logcat的:
12-20 08:17:32.842: W/ResourceType(1280): No package identifier when getting value for resource number 0x00000001
12-20 08:17:32.842: D/AndroidRuntime(1280): Shutting down VM
12-20 08:17:32.852: W/dalvikvm(1280): threadid=1: thread exiting with uncaught exception (group=0xb2a9eba8)
12-20 08:17:32.872: E/AndroidRuntime(1280): FATAL EXCEPTION: main
12-20 08:17:32.872: E/AndroidRuntime(1280): Process: com.example.test, PID: 1280
12-20 08:17:32.872: E/AndroidRuntime(1280): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1
12-20 08:17:32.872: E/AndroidRuntime(1280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-20 08:17:32.872: E/AndroidRuntime(1280): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-20 08:17:32.872: E/AndroidRuntime(1280): at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-20 08:17:32.872: E/AndroidRuntime(1280): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-20 08:17:32.872: E/AndroidRuntime(1280): at android.os.Handler.dispatchMessage(Handler.java:102)
12-20 08:17:32.872: E/AndroidRuntime(1280): at android.os.Looper.loop(Looper.java:136)
12-20 08:17:32.872: E/AndroidRuntime(1280): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-20 08:17:32.872: E/AndroidRuntime(1280): at java.lang.reflect.Method.invokeNative(Native Method)
12-20 08:17:32.872: E/AndroidRuntime(1280): at java.lang.reflect.Method.invoke(Method.java:515)
12-20 08:17:32.872: E/AndroidRuntime(1280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-20 08:17:32.872: E/AndroidRuntime(1280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-20 08:17:32.872: E/AndroidRuntime(1280): at dalvik.system.NativeStart.main(Native Method)
12-20 08:17:32.872: E/AndroidRuntime(1280): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
12-20 08:17:32.872: E/AndroidRuntime(1280): at android.content.res.Resources.getText(Resources.java:244)
12-20 08:17:32.872: E/AndroidRuntime(1280): at android.widget.TextView.setText(TextView.java:3888)
12-20 08:17:32.872: E/AndroidRuntime(1280): at com.example.test.MainActivity.onCreate(MainActivity.java:18)
12-20 08:17:32.872: E/AndroidRuntime(1280): at android.app.Activity.performCreate(Activity.java:5231)
12-20 08:17:32.872: E/AndroidRuntime(1280): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-20 08:17:32.872: E/AndroidRuntime(1280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-20 08:17:32.872: E/AndroidRuntime(1280): ... 11 more
12-20 08:20:15.382: I/Process(1280): Sending signal. PID: 1280 SIG: 9
12-20 08:21:33.692: W/ResourceType(1319): No package identifier when getting value for resource number 0x00000001
12-20 08:21:33.692: D/AndroidRuntime(1319): Shutting down VM
12-20 08:21:33.702: W/dalvikvm(1319): threadid=1: thread exiting with uncaught exception (group=0xb2a9eba8)
12-20 08:21:33.722: E/AndroidRuntime(1319): FATAL EXCEPTION: main
12-20 08:21:33.722: E/AndroidRuntime(1319): Process: com.example.test, PID: 1319
12-20 08:21:33.722: E/AndroidRuntime(1319): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1
12-20 08:21:33.722: E/AndroidRuntime(1319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-20 08:21:33.722: E/AndroidRuntime(1319): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-20 08:21:33.722: E/AndroidRuntime(1319): at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-20 08:21:33.722: E/AndroidRuntime(1319): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-20 08:21:33.722: E/AndroidRuntime(1319): at android.os.Handler.dispatchMessage(Handler.java:102)
12-20 08:21:33.722: E/AndroidRuntime(1319): at android.os.Looper.loop(Looper.java:136)
12-20 08:21:33.722: E/AndroidRuntime(1319): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-20 08:21:33.722: E/AndroidRuntime(1319): at java.lang.reflect.Method.invokeNative(Native Method)
12-20 08:21:33.722: E/AndroidRuntime(1319): at java.lang.reflect.Method.invoke(Method.java:515)
12-20 08:21:33.722: E/AndroidRuntime(1319): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-20 08:21:33.722: E/AndroidRuntime(1319): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-20 08:21:33.722: E/AndroidRuntime(1319): at dalvik.system.NativeStart.main(Native Method)
12-20 08:21:33.722: E/AndroidRuntime(1319): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
12-20 08:21:33.722: E/AndroidRuntime(1319): at android.content.res.Resources.getText(Resources.java:244)
12-20 08:21:33.722: E/AndroidRuntime(1319): at android.widget.TextView.setText(TextView.java:3888)
12-20 08:21:33.722: E/AndroidRuntime(1319): at com.example.test.MainActivity.onCreate(MainActivity.java:18)
12-20 08:21:33.722: E/AndroidRuntime(1319): at android.app.Activity.performCreate(Activity.java:5231)
12-20 08:21:33.722: E/AndroidRuntime(1319): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-20 08:21:33.722: E/AndroidRuntime(1319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-20 08:21:33.722: E/AndroidRuntime(1319): ... 11 more
答案 0 :(得分:0)
我解决了它
int x = Camera.getNumberOfCameras();
v.setText(String.valueOf(x));