gregkorossy / Android-Support-Preference-V7-Fix

Android androidx.preference support library has some issues, this lib tries to fix them.
https://discord.gg/87NVsSK
Apache License 2.0
497 stars 46 forks source link

SeekBarCompat crashes on 25.3.1.0 #75

Closed thearaks closed 7 years ago

thearaks commented 7 years ago

build.gradle:

...
    compile "com.android.support:preference-v7:25.3.1"
    compile 'com.android.support:preference-v14:25.3.1'
    compile 'com.takisoft.fix:preference-v7:25.3.1.0'
...

theme.xml:

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorBlue</item>
        <item name="colorPrimaryDark">@color/colorBlueDark</item>
        <item name="colorAccent">@color/colorOrange</item>

        <item name="preferenceTheme">@style/AppTheme.PreferenceTheme</item>
    </style>

    <style name="AppTheme.PreferenceTheme" parent="PreferenceFixTheme.Light">
        <item name="colorPrimary">@color/colorBlue</item>
        <item name="colorPrimaryDark">@color/colorBlueDark</item>
        <item name="colorAccent">@color/colorOrange</item>
    </style>

</resources>

And this is the preference XML:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <SeekBarPreference
        android:title="SeekBar Title"
        android:key="seekbarkey"
        android:defaultValue="50" />

</PreferenceScreen>

This immediately leads to a crash:

04-27 12:38:10.764 19392-19392/it.brc.dummy E/AndroidRuntime: FATAL EXCEPTION: main
  Process: it.brc.dummy, PID: 19392
  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference
      at android.support.v7.preference.SeekBarPreference.onBindViewHolder(SeekBarPreference.java:154)
      at android.support.v7.preference.PreferenceGroupAdapter.onBindViewHolder(PreferenceGroupAdapter.java:361)
      at android.support.v7.preference.PreferenceGroupAdapter.onBindViewHolder(PreferenceGroupAdapter.java:42)
      at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
      at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6389)
      at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)
      at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)
      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
      at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
      at android.view.View.layout(View.java:17641)
      at android.view.ViewGroup.layout(ViewGroup.java:5575)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
      at android.view.View.layout(View.java:17641)
      at android.view.ViewGroup.layout(ViewGroup.java:5575)
      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
      at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
      at android.view.View.layout(View.java:17641)
      at android.view.ViewGroup.layout(ViewGroup.java:5575)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
      at android.view.View.layout(View.java:17641)
      at android.view.ViewGroup.layout(ViewGroup.java:5575)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
      at android.view.View.layout(View.java:17641)
      at android.view.ViewGroup.layout(ViewGroup.java:5575)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
      at android.view.View.layout(View.java:17641)
      at android.view.ViewGroup.layout(ViewGroup.java:5575)
      at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
      at android.view.View.layout(View.java:17641)
      at android.view.ViewGroup.layout(ViewGroup.java:5575)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
      at android.view.View.layout(View.java:17641)
      at android.view.ViewGroup.layout(ViewGroup.java:5575)
      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
      at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
      at android.view.View.layout(View.java:17641)
      at android.view.ViewGroup.layout(ViewGroup.java:5575)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
      at com.android.internal.policy.DecorView.onLayout(DecorView.java:727)
04-27 12:38:10.765 19392-19392/it.brc.dummy E/AndroidRuntime:     at android.view.View.layout(View.java:17641)
      at android.view.ViewGroup.layout(ViewGroup.java:5575)
      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6344)
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
      at android.view.Choreographer.doCallbacks(Choreographer.java:686)
      at android.view.Choreographer.doFrame(Choreographer.java:621)
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
      at android.os.Handler.handleCallback(Handler.java:751)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6128)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

This happens on both Android 4.4.2 and 7.1.2.

It looks like not all the widget were inflated (i.e. R.id.seekbar and _R.id.seekbarvalue) or that the wrong layout was inflated.

Or, maybe... have I misconfigured something?

gregkorossy commented 7 years ago

First of all, you don't need these lines in your build.gradle as the fix already links them:

compile "com.android.support:preference-v7:25.3.1"
compile 'com.android.support:preference-v14:25.3.1'

Secondly, the styling doesn't look good to me. PreferenceFixTheme contains the preferenceTheme attribute itself because it is meant to be used as the "root" style, not the "preferenceTheme" style. Try the following solution instead (removed the preferenceTheme attribute and changed the parent theme to the fixed theme of your AppTheme):

    <style name="AppTheme" parent="@style/PreferenceFixTheme.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorBlue</item>
        <item name="colorPrimaryDark">@color/colorBlueDark</item>
        <item name="colorAccent">@color/colorOrange</item>
    </style>
thearaks commented 7 years ago

Thank you very much @Gericop. It obviously works now!

README.md wasn't very clear about this point (at least for me, maybe because I'm not a native speaker). A quick example of an AppTheme that extends PreferenceFixTheme might help, what do you think about it?

gregkorossy commented 7 years ago

Well, the sample app is supposed to show the basic usage, but I guess it won't hurt if I add an extra few lines to the README file.