我有一个 Activity,我已将 Activity 的内容 View 设置为“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);
}
但是我的应用程序崩溃了..我该如何解决这个问题。
最佳答案
当您第二次调用 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);
}
关于android - 在 Activity 中使用 GlSurfaceview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8851036/