代码之家  ›  专栏  ›  技术社区  ›  T. Markle

android opengl扩展glsurfaceview空指针异常

  •  5
  • T. Markle  · 技术社区  · 14 年前

    我正在尝试为Android创建一个简单的3-D应用程序,它将在OpenGL视图之上有一个额外的视图分层(很像API演示中的SurfaceView覆盖示例)。我在尝试用扩展的glsurfaceView类实现该方法时遇到了一个问题。我已经举了一个例子,我试图结合 this demo 使用API oerlay演示。如果我尝试像这样投射到Martin的VortExView对象(替换API演示中的第44-46行)

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

    我得到了一个ClassCastException错误(这是可以理解的,因为我认为Casting是相当具体的),所以我想我正在寻找一种方法来将视图从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源的情况下调试它们?

    1 回复  |  直到 14 年前
        1
  •  1
  •   T. Markle    14 年前

    以下是我的工作方式:

    在XML文件(mine is 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
    

    视图定义(VoReVIEW.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定义的视图,这正是我首先想要的。

    干杯!