Android OpenGL扩展GLSurfaceView空指针异常

发布于 2024-09-12 23:23:07 字数 1192 浏览 5 评论 0 原文

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

VortexView glSurfaceView=
     (VortexView) findViewById(R.id.glsurfaceview);

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

编辑: 我已经取得了一些进展,试图让它发挥作用- 在 API 示例中,视图 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" />

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

I am trying to create a simple 3-D app for android that will have an additional view layered on top of the OpenGL view (much like the SurfaceViewOverlay example in the API demos). I'm running into an issue trying to implement that method with an extended GLSurfaceView class. I've set up an example where I'm trying to do a combination of this demo with the API Oerlay demo. If I try to cast to a Martin's VortexView object like this (replace lines 44-46 in the API demo)

VortexView glSurfaceView=
     (VortexView) findViewById(R.id.glsurfaceview);

I get a ClassCastException error (which is understandable, as I assume casting is fairly specific) so I guess I'm looking for a method to transfer the view from a GLSurfaceView instance to a new subclass or a way to set the rendering surface to an XML defined view for a subclass after it has been created.

EDIT:
I've made some progress trying to get this to work-
in the API example the view XML uses
(from ApiDemos/res/layout/surface_view_overlay.xml)

        <android.opengl.GLSurfaceView android:id="@+id/glsurfaceview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

If I change that element to a
com.domain.project.VortexView
it will do the casting correctly with the above code but generates Null Pointer Exceptions when it hits the surfaceCreated and surfaceChanged routines (I think it's the called methods in the GLThread class based on the line number) inside the GLSurfaceView class. So maybe I should change the question-
How can I implement an extension for GLSurfaceView without generating NullPointerExceptions on surfaceCreated and surfaceChanged, or how can I debug them without having the source for GLSurfaceView.java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2024-09-19 23:23:07

以下是我让它工作的方法:

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

        <com.domain.project.VortexView android:id="@+id/vortexview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

在您的活动类中:

    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

视图定义 (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;
}

}

VortexRenderer.java 只有典型的 onSurfaceXXXXX 调用。

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

干杯!

Here's how I got it to work:

in the XML file (mine is main.xml) use the extended class spec

        <com.domain.project.VortexView android:id="@+id/vortexview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

In your activity class:

    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

The view definition (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;
}

}

the VortexRenderer.java just has typical onSurfaceXXXXX calls.

Anyway this seems to allow me to stack other XML defined views over my extended GLSurface which is what I wanted in the first place.

Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文