【问题标题】:surfaceview + glsurfaceview + framelayout表面视图 + glsurfaceview + 框架布局
【发布时间】:2010-01-21 10:57:51
【问题描述】:

我是 java 和 OpenGL 的新手。

我正在尝试获取具有以下功能的相机预览屏幕 同时显示 3d 对象。在浏览了样本后 api演示,我想结合示例的代码 api 演示就足够了。但不知何故它不起作用。强迫我 在启动时关闭并且错误被称为空指针 例外。有人可以与我分享我哪里出错以及如何解决 从那里开始。我如何对代码进行组合如图所示 下面:

myoverview.xml


<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <android.opengl.GLSurfaceView 
            android:id="@+id/cubes" 
            android:orientation="horizontal" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"/> 
    <SurfaceView 
            android:id="@+id/camera" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"/> 
</FrameLayout>

myoverview.java


import android.app.Activity; 
import android.os.Bundle; 
import android.view.SurfaceView; 
import android.view.Window; 
public class MyOverView extends Activity { 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       // Hide the window title. 
       requestWindowFeature(Window.FEATURE_NO_TITLE); 
       // camera view as the background 
       SurfaceView cameraView = (SurfaceView) findViewById(R.id.camera); 
       cameraView = new CameraView(this); 
       // visual of both cubes 
       GLSurfaceView cubesView = (GLSurfaceView) findViewById(R.id.cubes); 
       cubesView = new GLSurfaceView(this); 
       cubesView.setRenderer(new CubeRenderer(false)); 
       // set view 
       setContentView(R.layout.myoverview); 
    } 
}

GLSurfaceView.java


import android.content.Context; 
class GLSurfaceView extends android.opengl.GLSurfaceView { 
    public GLSurfaceView(Context context) { 
            super(context); 
    } 
} 

注意:

  • 我没有列出其余文件,因为它们只是 api 演示。 cameraView 指的是 camerapreview.java 示例 CubeRenderer 指的是 CubeRenderer.java 和 Cube.java 例子。任何帮助,将不胜感激。

  • 抱歉,由于格式错误,没有意识到编码不合适。

【问题讨论】:

    标签: android surfaceview


    【解决方案1】:

    您在使用 .xml 时遇到空指针异常的原因是因为您实际上是在您的 java 代码中创建新视图.. 而不是使用您可能已传入属性的 .xml 文件中的视图(如果您确实传递了以下属性..)..新视图显然会有一个空值..因此抛出一个空指针异常......例如 -

    cubesView = new GLSurfaceView(this);

    如果您已经在包含 FrameLayout 的 .xml 文件中创建了视图,则代码中实际上不需要。

    【讨论】:

      【解决方案2】:

      其实这很简单...如果你想在 XML 中定义你的视图,你只需要实现

      Public GLSurfaceView(Context context, AttributeSet attrs) {
      ...
      super(context, attrs);
      }
      

      而不是 GLSurfaceView(上下文上下文)

      这是在从 XML 初始化视图时自动调用的那个。我有同样的问题,这就是它是如何解决的。

      【讨论】:

        【解决方案3】:

        找到了如何解决它...通过 java 方式...只需使用 addContentView 而不是使用 xml....至少它已解决。 :)

        【讨论】:

          【解决方案4】:

          我实际上是在this SO link 中这样做的,它提供了一个完整的实现。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-11-20
            • 1970-01-01
            • 2011-11-21
            • 1970-01-01
            • 2014-06-27
            • 1970-01-01
            • 2011-06-05
            相关资源
            最近更新 更多