Android xml样式引用属性崩溃

时间:2016-01-14 09:56:09

标签: android xml crash attributes

在我的应用程序中,我们有一个显示的视图而不是ListView,以指示列表视图为空。它是从以下xml中膨胀的:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/view_empty_state_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tint="?colorAccent" />

        <Space
            android:layout_width="match_parent"
            android:layout_height="16dp" />

        <TextView
            android:id="@+id/view_empty_state_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textAllCaps="true"
            android:textColor="?colorAccent"
    android:textSize="@dimen/text_xxlarge" />

        <Space
            android:layout_width="match_parent"
            android:layout_height="24dp" />

    </LinearLayout>

最近,应用程序开始崩溃,使这个视图膨胀,产生了这个怪物堆栈:

    android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class <unknown>
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                    at android.view.View.inflate(View.java:19790)
                                                                    at <application package name>.view.EmptyStateView.init(EmptyStateView.java:58)
                                                                    at <application package name>.view.EmptyStateView.<init>(EmptyStateView.java:33)
                                                                    at <application package name>.adapter.ObservationsAdapter.onCreateViewHolder(ObservationsAdapter.java:187)
                                                                    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5223)
                                                                    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4449)
                                                                    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4359)
                                                                    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
                                                                    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
                                                                    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
                                                                    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
                                                                    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
                                                                    at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1418)
                                                                    at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1482)
                                                                    at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1399)
                                                                    at <application package name>.fragment.observations.AbstractListFragment$5.success(AbstractListFragment.java:242)
                                                                    at <application package name>.fragment.observations.AbstractListFragment$5.success(AbstractListFragment.java:193)
                                                                    at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                 Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
                                                                    at android.view.LayoutInflater.createView(LayoutInflater.java:645)
                                                                    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
                                                                    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                    at android.view.View.inflate(View.java:19790) 
                                                                    at <application package name>.view.EmptyStateView.init(EmptyStateView.java:58) 
                                                                    at <application package name>.view.EmptyStateView.<init>(EmptyStateView.java:33) 
                                                                    at <application package name>.adapter.ObservationsAdapter.onCreateViewHolder(ObservationsAdapter.java:187) 
                                                                    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5223) 
                                                                    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4449) 
                                                                    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4359) 
                                                                    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961) 
                                                                    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370) 
                                                                    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333) 
                                                                    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562) 
                                                                    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900) 
                                                                    at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1418) 
                                                                    at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1482) 
                                                                    at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1399) 
                                                                    at <application package name>.fragment.observations.AbstractListFragment$5.success(AbstractListFragment.java:242) 
                                                                    at <application package name>.fragment.observations.AbstractListFragment$5.success(AbstractListFragment.java:193) 
                                                                    at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45) 
                                                                    at android.os.Handler.handleCallback(Handler.java:739) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                    at android.os.Looper.loop(Looper.java:148) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                 Caused by: java.lang.reflect.InvocationTargetException
                                                                    at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                    at android.view.LayoutInflater.createView(LayoutInflater.java:619)
                                                                    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
                                                                    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                    at android.view.View.inflate(View.java:19790) 
                                                                    at <application package name>.view.EmptyStateView.init(EmptyStateView.java:58) 
                                                                    at <application package name>.view.EmptyStateView.<init>(EmptyStateView.java:33) 
                                                                    at <application package name>.adapter.ObservationsAdapter.onCreateViewHolder(ObservationsAdapter.java:187) 
                                                                    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5223) 
                                                                    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4449) 
                                                                    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4359) 
                                                                    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961) 
                                                                    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370) 
                                                                    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333) 
                                                                    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562) 
                                                                    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900) 
                                                                    at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1418) 
                                                                    at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1482) 
                                                                    at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1399) 
                                                                    at <application package name>.fragment.observations.AbstractListFragment$5.success(AbstractListFragment.java:242) 
                                                                    at <application package name>.fragment.observations.AbstractListFragment$5.success(AbstractListFragment.java:193) 
                                                                    at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45) 
                                                                    at android.os.Handler.handleCallback(Handler.java:739) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                    at android.os.Looper.loop(Looper.java:148) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                 Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x7f01011a a=-1}
                                                                    at android.content.res.TypedArray.getColorStateList(TypedArray.java:482)
                                                                    at android.widget.ImageView.<init>(ImageView.java:179)
                                                                    at android.widget.ImageView.<init>(ImageView.java:140)
                                                                    at android.widget.ImageView.<init>(ImageView.java:136)
                                                                    at java.lang.reflect.Constructor.newInstance(Native Method) 
                                                                    at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
                                                                    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
                                                                    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                    at android.view.View.inflate(View.java:19790) 
                                                                    at <application package name>.view.EmptyStateView.init(EmptyStateView.java:58) 
                                                                    at <application package name>.view.EmptyStateView.<init>(EmptyStateView.java:33) 
                                                                    at <application package name>.adapter.ObservationsAdapter.onCreateViewHolder(ObservationsAdapter.java:187) 
                                                                    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5223) 
                                                                    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4449) 
                                                                    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4359) 
                                                                    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961) 
                                                                    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370) 
                                                                    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333) 
                                                                    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562) 
                                                                    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900) 
                                                                    at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1418) 
                                                                    at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1482) 
                                                                    at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1399) 
                                                                    at <application package name>.fragment.observations.AbstractListFragment$5.success(AbstractListFragment.java:242) 
                                                                    at <application package name>.fragment.observations.AbstractListFragment$5.success(AbstractListFragment.java:193) 
                                                                    at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45) 
                                                                    at android.os.Handler.handleCallback(Handler.java:739) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                    at android.os.Looper.loop(Looper.java:148) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

很明显,问题是由&#34; tint&#34;中的样式引用引起的。 ImageView和TextView的属性(&#34;?colorAccent&#34;),因此我们将此引用更改为colors.xml文件的静态引用(现在属性值为&#34; @ color / accent&#34;)但是我很好奇什么可能导致问题首先

2 个答案:

答案 0 :(得分:1)

你使用了错误的符号。它不是

?colorAccent

您需要使用

?attr/colorAccent

代替。

答案 1 :(得分:0)

您可以直接将@color/colorPrimary用于您定义颜色的颜色 -

<color name="colorPrimary">#3F51B5</color>

的价值观。或者您需要将属性添加为

<attr name="colorPrimary" format="reference"/>

然后初始化为样式 -

<style name="AppTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
 <item name="attr/colorPrimary">@color/colorPrimary</item>
</style>

您可以使用?attr/colorPrimary