3

由于我的智能手机上从 Android 4.1 到 4.2 的新更新,我的应用程序在初始化 ViewPager 及其活动时引发异常。

例外:

java.lang.IllegalStateException: 此LayerDrawable 的一个或多个孩子没有恒定状态;这个drawable不能被改变。

堆栈跟踪:

java.lang.RuntimeException: Unable to start activity ComponentInfo{imatuve.info/imatuve.news.NewsTabActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class <unknown>
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
   at android.app.ActivityThread.access$600(ActivityThread.java:141)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5039)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   at dalvik.system.NativeStart.main(Native Method)Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class <unknown>
   at android.view.LayoutInflater.createView(LayoutInflater.java:613)
   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
   at imatuve.news.NewsTabActivity.createTabView(NewsTabActivity.java:313)
   at imatuve.news.NewsTabActivity.initialiseTabHost(NewsTabActivity.java:261)
   at imatuve.news.NewsTabActivity.onCreate(NewsTabActivity.java:96)
   at android.app.Activity.performCreate(Activity.java:5104)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
   ... 11 more
    Caused by: java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Constructor.constructNative(Native Method)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
   at android.view.LayoutInflater.createView(LayoutInflater.java:587)
... 23 more
    Caused by: android.content.res.Resources$NotFoundException: File res/drawable/tab_bg_selector.xml from drawable resource ID #0x7f020065
   at android.content.res.Resources.loadDrawable(Resources.java:1953)
   at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
   at android.view.View.<init>(View.java:3328)
   at android.view.View.<init>(View.java:3257)
   at android.view.ViewGroup.<init>(ViewGroup.java:425)
   at android.widget.RelativeLayout.<init>(RelativeLayout.java:210)
   ... 26 more
    Caused by: java.lang.IllegalStateException: One or more children of this LayerDrawable does not have constant state; this drawable cannot be mutated.
   at android.graphics.drawable.LayerDrawable.mutate(LayerDrawable.java:579)
   at android.graphics.drawable.DrawableContainer.selectDrawable(DrawableContainer.java:315)
   at android.graphics.drawable.StateListDrawable.onStateChange(StateListDrawable.java:106)
   at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:189)
   at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:885)
   at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
   at android.content.res.Resources.loadDrawable(Resources.java:1950)
   ... 31 more

编码:

private static View createTabView(final Context context, final String tag) {

            //The exception comes when inflating the view
            View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);

    TextView tv = (TextView)view.findViewById(R.id.tabsText);
    tv.setText(tag);

    return view;
}

tab_bg_selector.xml:

<?xml version="1.0" encoding="utf-8"?>

<!--  Active tab -->
<item android:state_selected="true" android:state_focused="false"
    android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
<!--  Inactive tab -->
<item android:state_selected="false" android:state_focused="false"
    android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />
<!--  Pressed tab -->
<item android:state_pressed="true" android:drawable="@drawable/tab_bg_pressed" />
<!--  Selected tab  -->
<item android:state_focused="true" android:state_selected="true"
    android:state_pressed="false" android:drawable="@color/transparent" />

tab_pg_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >   
<item
    android:bottom="8dp"
    android:top="63dp">
    <shape android:shape="rectangle" >
        <solid android:color="#70c0e7fd" />
    </shape>
</item>

<!-- mask out the bottom line of the tab shape -->
<item
    android:left="15dp"
    android:right="15dp"
    android:top="63dp">
    <shape android:shape="rectangle" >
        <corners android:bottomLeftRadius="0dp"
             android:bottomRightRadius="0dp" 
             android:topLeftRadius="3dp"
             android:topRightRadius="3dp" />

        <solid android:color="#70c0e7fd" />
    </shape>
</item>  

tab_bg_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:bottom="8dp"
    android:top="63dp">
    <shape android:shape="rectangle" >
        <solid android:color="#ffc0e7fd" />
    </shape>
</item>

<!-- mask out the bottom line of the tab shape -->
<item
    android:left="15dp"
    android:right="15dp"
    android:top="63dp">
    <shape android:shape="rectangle" >
        <corners android:bottomLeftRadius="0dp"
             android:bottomRightRadius="0dp" 
             android:topLeftRadius="3dp"
             android:topRightRadius="3dp" />

        <solid android:color="#ffc0e7fd" />
    </shape>
</item> 

tab_bg_unselected.xml

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

“tabs_bg”布局存在于我的布局文件夹中,就像 res/drawable/tab_bg_selector.xml 一样。

在 Android 4.1 或更低版本的操作系统上不会发生此异常。这可能是什么,我该如何摆脱它?

4

1 回答 1

3

我发现了问题。它位于 tab_bg_unselected.xml 中。

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

显然,在 Android 4.2 中,layer-list 必须至少有一个孩子。所以我在列表中添加了一个项目,应用程序不会再次抛出异常!

但仅添加一个“空”项是不够的!它还必须是内容,例如形状。

于 2012-11-30T08:40:37.743 回答