在活动中使用GlSurfaceview

时间:2012-01-13 13:05:10

标签: android opengl-es android-view glsurfaceview

我有一个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 个答案:

答案 0 :(得分: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);
 }