Occasional Binary XML file line #XX: Error inflating class <unknown>

雨燕双飞 提交于 2019-12-12 12:11:53

问题


I have a main activity, that holds the main menu. This menu has an option to start a second activity, which is a SurfaceView descendent.

I'm getting this error several times, but not always. I need to perform the process of calling the second activity via the first activity's menu button, and then returning to the first activity. Eventually (Usually on the 7th repetition), the error happens when the 2nd. activity is being launched. Whitout the debugger, the phone screen becomes black and is blocked for about 30 or more seconds, then I see the dialog to close it. In debugger, the app stops on this exception.

My layout file for the second activity is:

<?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>

I have a Class MySecActivity, with loads this layout on constructor.

I figured out that I'm getting an InflateException on setContentView. I'm checking the id I pass to setContentView and it's the same, not null, on all conditions:

int id = getResources().getIdentifier("mylayout", "layout", getPackageName());

if (id<= 0) {
    id= 0;  // just for debugging
} else {
   try {
       setContentView(id);
    } catch (InflateException e) {
       error = true;
    }
}

回答1:


You need to have the <?xml .. ?> prelude as well as set your xml namespace. Are you doing this?

<?xml version="1.0" encoding="utf-8"?>
<com.myapp.MySecActivity
    xmlns:android="http://schemas.android.com/apk/res/android"
    ...

If com.myapp.MySecActivity is not your root element, then try pasting your entire layout if you'd us to look at it.



来源:https://stackoverflow.com/questions/5231865/occasional-binary-xml-file-line-xx-error-inflating-class-unknown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!