2012-11-27 18 views
3

由於Android 4.1到4.2的新智能手機更新,我的應用程序在初始化ViewPager及其活動時會引發異常。Android 4.2:IllegalstateException:此LayerDrawable的一個或多個子項不具有常量狀態

例外:

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 /繪製/ tab_bg_selector.xml。

在Android 4.1 OS或更低版本上,不會發生此異常。 這可能是什麼,我該如何擺脫它?

+0

你能分享'tab_bg_selector.xml'嗎? – fiddler

+0

我在4.2中看到了與代碼中可繪製圖層相同的東西...我仍然需要調查發生了什麼。 – botteaap

回答

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中,圖層列表必須至少有一個孩子。所以我將一個項目添加到列表中,並且該應用程序不會再次拋出異常!

但是添加'空'項目是不夠的!它也必須是一個內容,就像一個形狀等等。

相關問題