仅在Android 4.X

时间:2016-09-30 08:14:07

标签: android

在将问题标记为重复之前,我已经检查了其他答案,但没有任何效果。该应用程序适用于Android> = 5.0,但在Android 4.4.2和其他Android 4.x设备上可能会失败。

这是发生崩溃的地方:

 @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
       View itemView = LayoutInflater.from(context).inflate(itemLayout, parent, false);
       ..........................

build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion ANDROID_BUILD_TOOLS

defaultConfig {
    minSdkVersion 16
    targetSdkVersion ANDROID_TARGET_SDK
.......................
}


dependencies {
 compile 'com.android.support:appcompat-v7:23.1.0'
 compile 'com.android.support:design:23.1.0'
 compile 'com.android.support:recyclerview-v7:23.1.0'
 compile 'com.github.bumptech.glide:glide:3.5.2'
 .......................
}

堆栈跟踪

android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
                                                                        at android.view.LayoutInflater.createView(LayoutInflater.java:620)
                                                                        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                                                                        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                        at com.dyve.counting.FileExplorer.RecyclerAdapter.onCreateViewHolder(RecyclerAdapter.java:54)
                                                                        at com.dyve.counting.FileExplorer.RecyclerAdapter.onCreateViewHolder(RecyclerAdapter.java:18)
                                                                        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.GridLayoutManager.layoutChunk(GridLayoutManager.java:438)
                                                                        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.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
                                                                        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
                                                                        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
                                                                        at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
                                                                        at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                                                                        at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1133)
                                                                        at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:760)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1673)
                                                                        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
                                                                        at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1673)
                                                                        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
                                                                        at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                        at android.view.View.layout(View.java:14858)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:4646)
                                                                        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2035)
                                                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1792)
                                                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1047)

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/primary_color"
            app:expandedTitleMarginStart="@dimen/title_margin"
            app:layout_scrollFlags="scroll|exitUntilCollapsed" >

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize"
                app:contentInsetStart="@dimen/title_margin"
                app:layout_collapseMode="pin"
                android:background="@color/primary_color"/>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="48dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

xml文件的第一行是:<android.support.v4.widget.DrawerLayout

我似乎无法弄清楚为什么会这样,这让我疯狂。我非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

在花了两个愚蠢的日子搞清楚是什么问题后,我想出了原因。 :使用属性作为xml drawable中颜色的引用。在API&lt; 21上,你不能使用attrs在xml drawable中着色。所以唯一的方法是使用颜色资源的引用(@ color / YOURS_COLOR)或使用#RGB格式。所以如果你想使用xml drawable和主题依赖颜色,你应该为每个主题创建一个drawable;

解决方案:如果有人遇到他的异常,你应该抓住它并在变量窗口中查看它的原因,因为错误消息更明确的地方。

我将相对布局的背景从@ drawable / selectable_background.xml更改为其他颜色,现在一切正常。 enter image description here