Android OpenGL ES 2:GLSurface视图在SetContentView设置为XML文件时显示黑屏

Android OpenGL ES 2:GLSurface视图在SetContentView设置为XML文件时显示黑屏,android,opengl-es,opengl-es-2.0,glsurfaceview,Android,Opengl Es,Opengl Es 2.0,Glsurfaceview,我对OpenGL相当陌生,我的问题是当我执行SetContentView(“R.layout.main”)时什么都不会发生,但是当我执行SetContentView(“mGLView”)时,游戏显示得很好。我的main.xml文件包含一个表面视图,因此我认为它会显示出来,因为main.xml有一个表面视图 我怀疑有一种方法可以解决这个问题,那就是使用addview或findviewbyd,但是我的暗示已经失败了 相关主活动代码: public class OpenGLES20Activity e

我对OpenGL相当陌生,我的问题是当我执行
SetContentView(“R.layout.main”)
时什么都不会发生,但是当我执行
SetContentView(“mGLView”)
时,游戏显示得很好。我的main.xml文件包含一个表面视图,因此我认为它会显示出来,因为main.xml有一个表面视图

我怀疑有一种方法可以解决这个问题,那就是使用addview或findviewbyd,但是我的暗示已经失败了

相关主活动代码:

public class OpenGLES20Activity extends Activity {
private GLSurfaceView mGLView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGLView = new MyGLSurfaceView(this);
    setContentView(R.layout.main);
main.xml代码:

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" 
android:layout_height="wrap_content">

            <SurfaceView 
android:id="@+id/SurfaceView" 
android:layout_width="500dp" 
android:layout_height="478dp">
</SurfaceView>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Timer"
                android:id="@+id/Timer"/>
        </RelativeLayout>

    </FrameLayout>


非常感谢您的帮助。

您需要设置扩展的
GLSurfaceView
,而不是xml文件中的
SurfaceView
,因此只需将
SurfaceView
更改为
com.example.yourapp.MyGLSurfaceView

<com.example.yourapp.MyGLSurfaceView 
    android:id="@+id/SurfaceView" 
    android:layout_width="500dp" 
    android:layout_height="478dp"/>

您需要设置扩展的
GLSurfaceView
而不是xml文件中的
SurfaceView
,因此只需将
SurfaceView
更改为
com.example.yourapp.MyGLSurfaceView

<com.example.yourapp.MyGLSurfaceView 
    android:id="@+id/SurfaceView" 
    android:layout_width="500dp" 
    android:layout_height="478dp"/>


我已对代码进行了更改,现在出现以下错误:(31,19)错误:MyGLSurfaceView类中的构造函数MyGLSurfaceView无法应用于给定类型;必需:上下文,找到属性集:无参数原因:实际参数列表和形式参数列表长度不同您的构造函数需要是
MyGLSurfaceView(上下文上下文,属性集属性)
,如我链接您的文档中所示。我的构造函数设置为该值,但我仍然收到错误。如果这有帮助,我的IDE已经指出这是一个问题:mGLView=newmyglsurfaceview(this);是的,你应该删除那行我有mGLView.onResume()和mGLView.onResume(),我应该把它们设置成什么?由于MyGLSurfaceView.onResume()不起作用。我已在代码中进行了更改,现在出现以下错误:(31,19)错误:MyGLSurfaceView类中的构造函数MyGLSurfaceView无法应用于给定类型;必需:上下文,找到属性集:无参数原因:实际参数列表和形式参数列表长度不同您的构造函数需要是
MyGLSurfaceView(上下文上下文,属性集属性)
,如我链接您的文档中所示。我的构造函数设置为该值,但我仍然收到错误。如果这有帮助,我的IDE已经指出这是一个问题:mGLView=newmyglsurfaceview(this);是的,你应该删除那行我有mGLView.onResume()和mGLView.onResume(),我应该把它们设置成什么?正如MyGLSurfaceView.onResume()不起作用一样。