consp1racy / android-support-preference

Android Preferences according to Material design specs
Apache License 2.0
331 stars 49 forks source link

Uncaught exception using XpPreferenceFragment on Android 2.3.6. (4.1.2 and 5.1.1 ok) #29

Closed samsonho closed 8 years ago

samsonho commented 8 years ago

Hi,

I have been trying to use XpPreferenceFragment with great success on newer Android phones with (Samsung GT-I9100) 4.1.2 and (HTC Desire HD CyanogenMod 12.1-UNOFFICIAL) 5.1.1.

But it fails with the most basic contrived preferences xml resources file when addPreferencesFromResource(R.id.settings) is called inside onCreatePreferences2 on an Android (Samsung GT-I8510T) 2.3.6 phone.

If I include the settings.xml listed below it fails with the exception also listed below. However if I omit the CheckBoxPreference tag in the settings.xml I can see "Hello World".

Could you please help?

In my build.gradle in Android Studio 2.0 I am using the following compile directives.

compile 'net.xpece.android:support-preference:0.7.0'
compile 'net.xpece.android:support-preference-color:0.7.0@aar'

Here are the contents of my SettingsFragment.java

package au.com.company.product.UI;
import android.os.Bundle;
import android.support.v7.preference.XpPreferenceFragment;
import au.com.company.product.R;
public class SettingsFragment extends XpPreferenceFragment {
    @Override
    public void onCreatePreferences2(final Bundle savedInstanceState, final String rootKey) {
        // Load the settings from an XML resource
        addPreferencesFromResource(R.xml.settings);
    }
}

Here are the contents of my settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="HelloWorld">
        <CheckBoxPreference
            android:key="some_check_box"
            android:title="This will fail to load"/>
    </PreferenceCategory>
</PreferenceScreen>

Here is the callstack of the exception thrown.

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         android.view.InflateException: Binary XML file line #18: Error inflating class <unknown>
                                                                             at android.view.LayoutInflater.createView(LayoutInflater.java:518)
                                                                             at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
                                                                             at android.support.v7.preference.XpPreferenceGroupAdapter.onCreateViewHolder(XpPreferenceGroupAdapter.java:76)
                                                                             at android.support.v7.preference.XpPreferenceGroupAdapter.onCreateViewHolder(XpPreferenceGroupAdapter.java:16)
                                                                             at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5482)
                                                                             at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4707)
                                                                             at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
                                                                             at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
                                                                             at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
                                                                             at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
                                                                             at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
                                                                             at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
                                                                             at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
                                                                             at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
                                                                             at android.view.View.layout(View.java:7228)
                                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
                                                                             at android.view.View.layout(View.java:7228)
                                                                             at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
                                                                             at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
                                                                             at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
                                                                             at android.view.View.layout(View.java:7228)
                                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
                                                                             at android.view.View.layout(View.java:7228)
                                                                             at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
                                                                             at android.view.View.layout(View.java:7228)
                                                                             at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122)
                                                                             at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                                                                             at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1170)
                                                                             at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:814)
                                                                             at android.view.View.layout(View.java:7228)
                                                                             at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1187)
                                                                             at android.view.View.layout(View.java:7228)
                                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
                                                                             at android.view.View.layout(View.java:7228)
                                                                             at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
                                                                             at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
                                                                             at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
                                                                             at android.view.View.layout(View.java:7228)
                                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
                                                                             at android.view.View.layout(View.java:7228)
                                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
                                                                             at android.view.View.layout(View.java:7228)
                                                                             at android.view.ViewRoot.performTraversals(ViewRoot.java:1148)
                                                                             at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                             at android.os.Looper.loop(Looper.java:130)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:3691)
                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:507)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
                                                                             at dalvik.system.NativeStart.main(Native Method)
                                                                          Caused by: java.lang.reflect.InvocationTargetException
                                                                            at java.lang.reflect.Constructor.con

Many Thanks

eku commented 8 years ago

Check your XML preferences file.

E/AndroidRuntime: FATAL EXCEPTION: main android.view.InflateException: Binary XML file line #18: Error inflating class <unknown> at android.view.LayoutInflater.createView(LayoutInflater.java:518) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)

PhoneLayoutInflater.java does not match with the XML file you provided. Did you miss anything?

consp1racy commented 8 years ago

@samsonho Hi, unfortunately the stack is cut off where it starts to get interesting. Could you use SettingsFragment in a simpler activity with just a toolbar and a linear layout so the view hierarchy does not bloat stack and then post stack trace again? I need to see the rest. Thank you.

@eku Line 18 is probably start of asp_preference_category.xml. This is on my side. And since the sample app works on Android 2.3.7 it must be some sort of misconfiguration.

Make sure you're using AppCompat theme and all necessary attributes are defined, example:

<style name="Theme.MyApp" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="preferenceTheme">@style/PreferenceThemeOverlay.Material</item>
    <item name="preferenceTint">?colorAccent</item>
</style>

What version of support libs are you using?

samsonho commented 8 years ago

Hi @consp1racy and @eku,

Thanks for responding. I realised I have not loaded your sample onto this device 2.3.6 and I have tried it and it seems to work well. So the error is likely in my code.

I have noticed that you have specified preferenceThemeOverlay instead of preferenceTheme is that correct? I assume that it is ok for me to use Theme.AppCompat.Light.NoActionBar instead of Theme.AppCompat.Light.DarkActionBar?

In any case I will break down my problem and if I find something useful I will post it to this thread again. I will try putting my SettingsFragment with a toolbar into a simplier activity with a linear layout if I can't figure my problem soon.

Again thanks,

samsonho commented 8 years ago

Hi @consp1racy,

I have discovered what my problem was. I had to remove a couple things from my project. One was from my AndroidManifest.xml.

<uses-sdk xmlns:tools="http://schemas.android.com/tools" tools:overrideLibrary="android.support.v14.preference" />

Also I noticed that I was compiling for two preference libraries.

compile 'com.android.support:preference-v7:23.3.0' compile 'com.android.support:preference-v14:23.3.0'

So the other thing I had to remove was for com.android.support:preference-v14:23.3.0. That fixed my issue.

I hope that is helpful for those encountering the same issue when dealing with code remnants when porting from one Preference library to another.

Cheers,

consp1racy commented 8 years ago

Hi @samsonho,

preference-v14 probably overrides some preference-v7 layouts with ones that use attributes only available on API 14 or higher causing a crash on older devices.

Since this is an edge case easily prevented by not adding tools:override I will not investigate further.

preferenceThemeOverlay was a typo, thank you for pointing that out.

JoseGD commented 8 years ago

@samsonho it's not necessary to reference the preference-v7 libraries, if you want to use ASP you just reference 'net.xpece.android:support-preference-:0.7.0'. Unless you are planning to use some feature of the Google libraries not covered by ASP

consp1racy commented 8 years ago

@JoseGD Just to clarify ASP imports preference-v7 as a dependency.

JoseGD commented 8 years ago

@consp1racy that's what I meant, importing ASP makes importing preference-v7 redundant