当您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);
}