【问题标题】:OpenGL ES 2: GLSurface View Showing Black Screen When SetContentView is set to an XML FileOpenGL ES 2:当 SetContentView 设置为 XML 文件时,GLSurface 视图显示黑屏
【发布时间】:2016-12-31 23:57:55
【问题描述】:

我对 OpenGL 还很陌生,我的问题是当我执行 SetContentView("R.layout.main") 时没有任何反应,但是当我执行 SetContentView("mGLView")时,游戏显示正常。我的 main.xml 文件包含一个表面视图,因此我认为它会被显示,因为 main.xml 有一个表面视图。

我怀疑有一种方法可以解决此问题,将使用 addview 或 findviewbyid 完成,但是我的暗示失败了。

相关MainActivity代码:

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>

感谢任何帮助。

【问题讨论】:

    标签: android opengl-es opengl-es-2.0 glsurfaceview


    【解决方案1】:

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

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

    【讨论】:

    • 我已对代码进行了更改,现在出现以下错误 Error:(31, 19) error: MyGLSurfaceView 类中的构造函数 MyGLSurfaceView 无法应用于给定类型;必需:上下文,找到属性集:没有参数原因:实际参数列表和正式参数列表的长度不同
    • 您的构造函数必须是MyGLSurfaceView(Context context, AttributeSet attrs),如我已链接您的文档中所示。
    • 我的构造函数设置为那个,我仍然得到错误。如果这有助于我的 IDE 表明这是一个问题:mGLView = new MyGLSurfaceView(this);
    • 是的,你应该删除那行
    • 我有 mGLView.onResume() 和 mGLView.onResume() 我应该将它们设置为什么?因为 MyGLSurfaceView.onResume() 不起作用。
    猜你喜欢
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    相关资源
    最近更新 更多