Android OpenGL扩展了GLSurfaceView空指针异常

T. *_*kle 5 3d android views opengl-es surfaceview

我正在尝试为Android创建一个简单的3-D应用程序,它将在OpenGL视图之上分层一个额外的视图(很像API演示中的SurfaceViewOverlay示例).我遇到了一个试图用扩展的GLSurfaceView类实现该方法的问题.我已经设置了一个示例,我正在尝试将此演示与API Oerlay演示相结合.如果我尝试像这样转换到Martin的VortexView对象(替换API演示中的第44-46行)

VortexView glSurfaceView=
     (VortexView) findViewById(R.id.glsurfaceview);
Run Code Online (Sandbox Code Playgroud)

我得到一个ClassCastException错误(这是可以理解的,因为我假设转换是相当具体的)所以我想我正在寻找一种方法将视图从GLSurfaceView实例传输到新的子类或将渲染表面设置为创建子类后的XML定义视图.

编辑:我已经取得了一些进展,试图让这个工作 - 在XML示例中使用的视图XML(来自ApiDemos/res/layout/surface_view_overlay.xml)

        <android.opengl.GLSurfaceView android:id="@+id/glsurfaceview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

如果我将该元素更改为
com.domain.project.VortexView,它将使用上面的代码正确地进行转换,但是当它到达surfaceCreated和surfaceChanged例程时会生成Null Pointer Exceptions(我认为它是基于GLThread类的被调用方法) GLSurfaceView类中的行号).所以也许我应该改变这个问题 - 如何在不在SurfaceCreated和surfaceChanged上生成NullPointerExceptions的情况下为GLSurfaceView实现扩展,或者如何在没有GLSurfaceView.java源的情况下调试它们?

T. *_*kle 1

这是我让它发挥作用的方法:

在 XML 文件(我的是 main.xml)中使用扩展类规范

        <com.domain.project.VortexView android:id="@+id/vortexview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
Run Code Online (Sandbox Code Playgroud)

在您的活动课中:

    setContentView(R.layout.main);
    VortexRenderer _renderer=new VortexRenderer();         // setup our renderer
    VortexView glSurface=(VortexView) findViewById(R.id.vortexview); // use the xml to set the view
   glSurface.setRenderer(_renderer); // MUST BE RIGHT HERE, NOT in the class definition, not after any other calls (see GLSurfaceView.java for related notes)
   glSurface.showRenderer(_renderer); // allows us to access the renderer instance for touch events, etc
Run Code Online (Sandbox Code Playgroud)

视图定义(VortexView.java):

public class VortexView extends GLSurfaceView {
    public VortexRenderer _renderer; // just a placeholder for now

public VortexView(Context context) { // default constructor
    super(context);
}


public VortexView(Context context, AttributeSet attrs) { /*IMPORTANT - this is the constructor that is used when you send your view ID in the main activity */
    super(context, attrs);
}

public void showRenderer(VortexRenderer renderer){ // sets our local object to the one created in the main activity, a poor man's getRenderer
    this._renderer=renderer;        
}

public boolean onTouchEvent(final MotionEvent event) { // An example touchevent from the vortex demo
    queueEvent(new Runnable() {
        public void run() {
           _renderer.setColor(event.getX() / getWidth(), event.getY() / getHeight(), 1.0f);
        }
    });
    return true;
}
Run Code Online (Sandbox Code Playgroud)

}

VortexRenderer.java 仅具有典型的 onSurfaceXXXXX 调用。

无论如何,这似乎允许我在扩展的 GLSurface 上堆叠其他 XML 定义的视图,这正是我首先想要的。

干杯!