Reputation: 357
I'm trying to use a ColorStateList to colorize the elements in my RecyclerView. Here's the xml for my ViewHolder:
<?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="wrap_content"
android:orientation="vertical"
android:background="@drawable/view_colors"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8sp"
android:id="@+id/entry_image"
android:layout_gravity="center_horizontal"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8sp"
android:id="@+id/entry_text"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
And here's my view_colors.xml, which holds the Color State List:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@color/colorPrimary"/>
<item android:state_focused="true" android:drawable="@color/colorAccent"/>
<item android:drawable="@color/colorPrimaryDark"/>
</selector>
When I used a ColorStateList in res/colors and applied it to a textColor tag, it worked fine. I tried the same with the background tag, and got this error. So I looked at other questions here and then tried to use an xml in res/drawable that defined drawables instead of colors (as seen above) but that didn't fix the issue.
EDIT: This works fine on my phone with API level 27, but the error shows up on my API level 23 phone.
Here's the full stack trace: 2019-04-12 13:47:39.419 22410-22410/com.example.realrecyclerview E/linker: readlink('') failed: No such file or directory [fd=31] 2019-04-12 13:47:39.419 22410-22410/com.example.realrecyclerview E/linker: warning: unable to get realpath for the library "/data/app/com.example.realrecyclerview-1/oat/arm/base.odex". Will use given name. 2019-04-12 13:47:41.305 22410-22410/com.example.realrecyclerview E/linker: readlink('') failed: No such file or directory [fd=38] 2019-04-12 13:47:41.305 22410-22410/com.example.realrecyclerview E/linker: warning: unable to get realpath for the library "/system/lib/hw/gralloc.sc8830.so". Will use given name. 2019-04-12 13:47:41.420 22410-22410/com.example.realrecyclerview E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.realrecyclerview, PID: 22410 android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class at android.view.LayoutInflater.inflate(LayoutInflater.java:539) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at com.example.realrecyclerview.HarvestAdapter.onCreateViewHolder(HarvestAdapter.java:48) at com.example.realrecyclerview.HarvestAdapter.onCreateViewHolder(HarvestAdapter.java:14) at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230) at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:557) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612) at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194) at android.view.View.layout(View.java:16645) at android.view.ViewGroup.layout(ViewGroup.java:5471) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1732) at android.widget.LinearLayout.onLayout(LinearLayout.java:1497) at android.view.View.layout(View.java:16645) at android.view.ViewGroup.layout(ViewGroup.java:5471) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16645) at android.view.ViewGroup.layout(ViewGroup.java:5471) at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444) at android.view.View.layout(View.java:16645) at android.view.ViewGroup.layout(ViewGroup.java:5471) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16645) at android.view.ViewGroup.layout(ViewGroup.java:5471) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16645) at android.view.ViewGroup.layout(ViewGroup.java:5471) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2694) at android.view.View.layout(View.java:16645) at android.view.ViewGroup.layout(ViewGroup.java:5471) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 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(Acti
Upvotes: 0
Views: 245
Reputation: 357
The issue was the entry in the selector that didn't have a state associated with it. I removed the following line and it works fine on all my devices:
<item android:drawable="@color/colorPrimaryDark"/>
Upvotes: 1
Reputation: 2476
The runtime exception usually contains the fault in the layout or XML file. Please give out the full LogCat exception trace.
Other than that, you mentioned you want a ColorStateList yet you're using a drawable. Haven't used colors in a drawable selector. If all you want is plain Colous, try a color selector instead. Same XML file, just move it to "color" Res folder and exchange android:drawable with android:color
Upvotes: 0