在Activity中使用GlSurfaceView

3

我有一个Activity,并将它的内容视图设置为“R.layout.main.xml”。我还有另一个类,使用OpenGL创建动画。现在我需要在Activity的后台使用这个动画。

代码如下:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_pixie);

    mGLView = new ClearGLSurfaceView(this);
    setContentView(mGLView);
 }

但我的应用程序崩溃了..我该怎么解决呢?
1个回答

4
当您第二次调用setContentView()时,您将替换第一次设置的内容,只剩下背景。崩溃最可能是因为您依赖于已被移除的主要布局中的元素。
与其两次调用setContentView(),不如将GLSurfaceView包含在主要布局中。以下是如何实现的示例:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent>
    <your.application.package.ClearGLSurfaceView
         android:layout_width="match_parent"
         android:layout_width="match_parent"/>
    <!--put the rest of your layout here, i.e the contents of the original R.layout.main_pixie-->
</FrameLayout>

然后你可以像往常一样在 onCreate() 中加载这个布局(main_pixie_new 是上面xml文件的引用,我只是为了让事情更加清晰明了而给它取了这个名字):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_pixie_new);
 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接