我正在制作一个支持从API 16到API 22的应用程序。我正在使用RecyclerView来显示项目列表。我的布局代码如下
<?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="56dp"
android:layout_gravity="center_vertical"
android:background="@drawable/ripple"
android:clickable="true"
android:orientation="horizontal"
android:paddingLeft="@dimen/default_padding"
android:paddingRight="@dimen/default_padding">
<ImageView
android:id="@+id/s_icon"
android:layout_width="@dimen/drawer_icon_size"
android:layout_height="@dimen/drawer_icon_size"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_sd"
android:tint="@color/storage_image_tint" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/drawer_item_label_margin"
android:layout_marginStart="@dimen/drawer_item_label_margin"
android:orientation="vertical">
<TextView
android:id="@+id/s_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="@color/storage_label_selector" />
<TextView
android:id="@+id/s_stat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="@string/hello_blank_fragment"
android:textColor="@color/storage_stat_selector"
android:textSize="@dimen/storage_stat_text" />
<ProgressBar
android:id="@+id/s_progress"
style="@style/CustomProgressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/storage"
android:progressDrawable="@drawable/storage" />
</LinearLayout>
</LinearLayout>
此代码在API 21和22中运行良好但是当我在API 16(JellyBean)中启动我的应用程序时,我的logcat显示以下错误。
07-27 05:58:42.115 11590-11596/? E/jdwp﹕ Failed sending reply to debugger: Broken pipe
07-27 05:58:42.487 11590-11590/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #12: 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.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.zaeron.filexplorer.Adapter$Storage.onCreateViewHolder(Adapter.java:84)
at com.zaeron.filexplorer.Adapter$Storage.onCreateViewHolder(Adapter.java:30)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5062)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4368)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4278)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1947)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1359)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1322)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:556)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2673)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2971)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.widget.ScrollView.onLayout(ScrollView.java:1438)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:931)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallba
我想弄清楚这一点,但我没有得到如何解决这个问题。顺便说一下,我是Android的新手。
编辑: 我的RecyclerView代码
<FrameLayout
android:layout_marginTop="8dp"
android:id="@+id/sRecyclerViewContainer"
style="@style/StorageRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/sRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never" />
</FrameLayout>
编辑: 我在代码中发现了问题。实际上,recyclerView没有任何问题,但这个recyclerView显示了设备中的所有存储。但是,当我在软糖上运行它时,很可能在获取设备的内部存储器时出现问题。那么任何人都可以帮助我如何在JellyBean中找到内部存储
我使用Environment. getExternalStorageDirectory()
获取存储目录
答案 0 :(得分:0)
我认为你已经创建了drawable或v21目录中的任何资源。确保您在默认资源目录中也拥有每个资源,这样当您处于较低版本时,您的应用程序就可以使用该默认资源。
答案 1 :(得分:-1)
你的布局应该是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</RelativeLayout>
你的代码:
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);