我有一个主要活动,它包含主菜单。这个菜单有一个选项来启动第二个活动,这是一个SurfaceView
后代。
我多次收到此错误,但并非总是如此。我需要执行通过第一个活动的菜单按钮调用第二个活动的过程,然后返回到第一个活动。最终(通常在第 7 次重复),错误发生在第 2 次时。活动正在启动。Whitout 调试器,手机屏幕变黑并被阻止大约 30 秒或更长时间,然后我看到关闭它的对话框。在调试器中,应用程序在此异常上停止。
我的第二个活动的布局文件是:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.myapp.MySecActivity
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/sec_view"/>
</FrameLayout>
我有一个 Class MySecActivity
,在构造函数上加载这个布局。
我发现我正在InflateException
上火setContentView
。我正在检查我传递给的 id setContentView
,它在所有条件下都是相同的,而不是 null:
int id = getResources().getIdentifier("mylayout", "layout", getPackageName());
if (id<= 0) {
id= 0; // just for debugging
} else {
try {
setContentView(id);
} catch (InflateException e) {
error = true;
}
}