简体   繁体   English

相机预览示例

[英]Camera Preview Example

I know this is really basic but it is a error that I cannot find a solution to. 我知道这确实很基本,但是这是我找不到解决方法的错误。

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

I have 2 errors and I dont know what Im doning wrong. 我有2个错误,我不知道我在做什么错。

public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate our menu which can gather user input for switching camera
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.camera_menu, menu);
    return true;
}

Im getting an error at "menu" in "R.menue..." 我在“ R.menue ...”中的“ menu”上遇到错误

error 2: 错误2:

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.switch_cam:
            // check for availability of multiple cameras
            if (numberOfCameras == 1) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                /**builder.setMessage(this.getString(R.string.camera_alert))
                       .setNeutralButton("Close", null);*/
                AlertDialog alert = builder.create();
                alert.show();
                return true;
}

Im getting an error on "id" in "case R.id..." 我在“ case R.id ...”中遇到“ id”错误

Thanks 谢谢

First, do you have : <uses-permission android:name="android.permission.CAMERA" /> in your AndroidManifest.xml ? 首先,您在AndroidManifest.xml是否具有: <uses-permission android:name="android.permission.CAMERA" />

You can try to check a full Camera preview here for Android : Camera Preview Android 您可以在此处尝试查看完整的相机预览:Android 相机预览

Another good website with source files : Using Camera API with SourceFile 另一个带有源文件的优秀网站: 将Camera API与SourceFile一起使用

You most likely just copied the source file you linked above into your project. 您很可能只是将上面链接的源文件复制到了项目中。 You also have to add res/menu/camera_menu.xml . 您还必须添加res/menu/camera_menu.xml This defines the options menu that comes up when you press the menu button. 这定义了按下菜单按钮时出现的选项菜单。 See the menu doc for more information how that works if you're interested. 如果您有兴趣,请参阅菜单文档以获取更多信息。

If you don't do that the tools notice that you miss a file that you reference in code but that's not actually there, which results the first error. 如果不这样做,这些工具会注意到您错过了在代码中引用的文件,但是实际上并没有该文件,这将导致第一个错误。 The second error is also caused by this in an indirect way. 第二个错误也是由此间接引起的。 The missing ID is also created within the menu file. 缺少的ID也会在菜单文件中创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM