3

我有一个活动,我将活动的内容视图设置为“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);
 }

但是我的应用程序崩溃了..我该如何解决这个问题。

4

1 回答 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);
 }
于 2012-01-13T13:14:10.493 回答