gpt4 book ai didi

安卓 4.2 : IllegalstateException: One or more children of this LayerDrawable does not have constant state

转载 作者:太空狗 更新时间:2023-10-29 12:49:44 26 4
gpt4 key购买 nike

自从我的智能手机从 Android 4.1 更新到 4.2 后,我的应用在初始化 ViewPager 及其 Activity 时抛出异常。

异常:

java.lang.IllegalStateException:此 LayerDrawable 的一个或多个子级没有常量状态;此可绘制对象无法更改。

StackTrace:

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 操作系统或更低版本上不会发生此异常。这可能是什么,我该如何摆脱它?

最佳答案

我发现了问题。它位于 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 中,图层列表必须至少有一个子项。所以我在列表中添加了一个项目,应用程序没有再次抛出异常!

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

关于安卓 4.2 : IllegalstateException: One or more children of this LayerDrawable does not have constant state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13580128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com