PreferenceFragmentCompat crashes on initalization
Asked Answered
G

4

5

I have made an application with a ViewPager. Everything is working fine, when I make a debug build, and test it on my phone. (Not emulator) But when I build a release .apk and install it, the PreferenceFragmentCompat's addPreferencesFromResource(R.xml.preferences); crashes the app. PreferenceFragmentCompat - Android Developers

ApplicationSettingsFragment.java:

package hu.t_bond.homecontroller.Fragments;

import android.os.Bundle;
import android.support.v7.preference.PreferenceFragmentCompat;

import hu.t_bond.homecontroller.Launcher;
import hu.t_bond.homecontroller.R;

public class ApplicationSettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        getPreferenceManager().setSharedPreferencesName(Launcher.SETTINGS_NAME);

        addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public void onCreatePreferences(Bundle bundle, String s)
    {
    }
}

res/xml/preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <CheckBoxPreference
        android:key="AutoUpdatesEnabled"
        android:title="@string/auto_updates_enabled"
        android:summary="@string/auto_updates_enabled_description"
        android:defaultValue="true" />
</PreferenceScreen>

And the crash report:

10-26 14:20:07.661 17763-17890/? E/AndroidRuntime: FATAL EXCEPTION: main
 Process: hu.t_bond.homecontroller, PID: 17763
 android.view.InflateException: Binary XML file line #2: Error inflating class PreferenceScreen
     at android.support.v7.preference.p.a(PreferenceInflater.java:261)
     at android.support.v7.preference.p.a(PreferenceInflater.java:280)
     at android.support.v7.preference.p.b(PreferenceInflater.java:289)
     at android.support.v7.preference.p.a(PreferenceInflater.java:161)
     at android.support.v7.preference.p.a(PreferenceInflater.java:117)
     at android.support.v7.preference.q.a(PreferenceManager.java:115)
     at android.support.v7.preference.j.b(PreferenceFragmentCompat.java:366)
     at hu.t_bond.homecontroller.b.a.d(ApplicationSettingsFragment.java:18)
     at android.support.v4.b.o.h(Fragment.java:1942)
     at android.support.v4.b.ac.a(FragmentManager.java:1040)
     at android.support.v4.b.ac.b(FragmentManager.java:926)
     at android.support.v4.b.o.c(Fragment.java:882)
     at android.support.v4.b.an.b(FragmentStatePagerAdapter.java:152)
     at android.support.v4.view.ViewPager.a(ViewPager.java:1104)
     at android.support.v4.view.ViewPager.a(ViewPager.java:552)
     at android.support.v4.view.ViewPager.a(ViewPager.java:514)
     at android.support.v4.view.ViewPager.b(ViewPager.java:946)
     at android.support.v4.view.dw.onChanged(ViewPager.java:2910)
     at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
     at android.support.v4.view.bo.c(PagerAdapter.java:276)
     at hu.t_bond.homecontroller.w.c(Launcher.java:948)
     at hu.t_bond.homecontroller.Launcher.a(Launcher.java:521)
     at hu.t_bond.homecontroller.q.onClick(Launcher.java:663)
     at android.view.View.performClick(View.java:4789)
     at android.view.View$PerformClick.run(View.java:19881)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:135)
     at android.app.ActivityThread.main(ActivityThread.java:5292)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
  Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
     at java.lang.Class.getConstructor(Class.java:531)
     at java.lang.Class.getConstructor(Class.java:495)
     at android.support.v7.preference.p.a(PreferenceInflater.java:248)
     at android.support.v7.preference.p.a(PreferenceInflater.java:280) 
     at android.support.v7.preference.p.b(PreferenceInflater.java:289) 
     at android.support.v7.preference.p.a(PreferenceInflater.java:161) 
     at android.support.v7.preference.p.a(PreferenceInflater.java:117) 
     at android.support.v7.preference.q.a(PreferenceManager.java:115) 
     at android.support.v7.preference.j.b(PreferenceFragmentCompat.java:366) 
     at hu.t_bond.homecontroller.b.a.d(ApplicationSettingsFragment.java:18) 
     at android.support.v4.b.o.h(Fragment.java:1942) 
     at android.support.v4.b.ac.a(FragmentManager.java:1040) 
     at android.support.v4.b.ac.b(FragmentManager.java:926) 
     at android.support.v4.b.o.c(Fragment.java:882) 
     at android.support.v4.b.an.b(FragmentStatePagerAdapter.java:152) 
     at android.support.v4.view.ViewPager.a(ViewPager.java:1104) 
     at android.support.v4.view.ViewPager.a(ViewPager.java:552) 
     at android.support.v4.view.ViewPager.a(ViewPager.java:514) 
     at android.support.v4.view.ViewPager.b(ViewPager.java:946) 
     at android.support.v4.view.dw.onChanged(ViewPager.java:2910) 
     at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37) 
     at android.support.v4.view.bo.c(PagerAdapter.java:276) 
     at hu.t_bond.homecontroller.w.c(Launcher.java:948) 
     at hu.t_bond.homecontroller.Launcher.a(Launcher.java:521) 
     at hu.t_bond.homecontroller.q.onClick(Launcher.java:663) 
     at android.view.View.performClick(View.java:4789) 
     at android.view.View$PerformClick.run(View.java:19881) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5292) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

And I'm using the newest libs:

compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:preference-v7:23.1.0'

Can somebody tell me what I'm doing wrong? Thanks

Galarza answered 26/10, 2015 at 13:33 Comment(10)
try to use res-auto instead of res/androidIgloo
@Igloo the same problem.Galarza
ok try to remove all the upperCase : preferencescreenIgloo
@Igloo no: android.view.InflateException: Binary XML file line #2: Error inflating class (not found)preferencescreenGalarza
from where did you get this code ?!! <PreferenceScreen> tag is not a default XML tag in android !!!Igloo
It is from the support library. I added the link to the description.Galarza
what's your build tool and your compileSDK ???Igloo
@Igloo Gradle 2.4 Build Tools Version 23.0.1 Compile SDK version: 23 (And I'm using Android Studio 1.5 Preview 2)Galarza
@Galarza do you have ProGuard on when building your APK?Tuberous
@Tuberous THANK YOU!! That fixed it. Put in an answer and I will accept it.Galarza
T
5

The OP said he is using ProGuard when building his APK file.

As this bug report states

Make sure your proguard configuration contains a line like this:

-keep public class * extends android.support.v7.preference.Preference

This might be the issue, although I'm not sure.

Tuberous answered 26/10, 2015 at 14:31 Comment(0)
J
3

Because of you imported android.support.v7.preference.PreferenceFragmentCompat, so you need <android.support.v7.preference.PreferenceScreen .../> and <android.support.v7.preference.CheckBoxPreference .../>. It will looks like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v7.preference.CheckBoxPreference
        android:key="AutoUpdatesEnabled"
        android:title="@string/auto_updates_enabled"
        android:summary="@string/auto_updates_enabled_description"
        android:defaultValue="true" />

</android.support.v7.preference.PreferenceScreen>

More information about these new preference types which is supported for v7 Support Library.

EDIT

Instead of overriding default onCreate(Bundle) method, but you should only override onCreatePreferences(Bundle, String):

@Override
public void onCreatePreferences(Bundle bundle, String s) {
    addPreferencesFromResource(R.xml.preferences);
}
Jackknife answered 26/10, 2015 at 14:17 Comment(7)
How could I missed that? :D But after repleacing the problem is the same: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.preference.PreferenceScreen (But the developer site said that I should use that way. And it worked in debug release) Maybe the ProGuard?Galarza
@T-bond, I edited my answer, and see what happens now.Jackknife
the problem is the same. :'( Doesn't the Caused by: java.lang.NoSuchMethodException could be a point to start of?Galarza
@Galarza how are you using your SettingsFragment? Do you maybe host it inside a layout via <fragment> tag?Tuberous
No. My FragmentStatePagerAdapter has: @Override public Fragment getItem(int position) { return new ApplicationSettingsFragment(); } The return statement in a switch with other fragments, and they are working correctly.Galarza
@T-bond, this post may helps you a lot: https://mcmap.net/q/175727/-how-to-use-the-v7-v14-preference-support-libraryJackknife
@AnggrayudiH there is nothing new in the link for me I think.Galarza
S
2

Fixing all proguard issues for the most recent androidx

-keep public class * extends androidx.preference.* { *;}
Schrock answered 3/9, 2019 at 8:33 Comment(1)
Why would this fix issues? Is there a bug report for this specifically?Refreshment
I
-1

try to add this constructor in your java code:

    PreferenceFragmentCompat(){
this.super();
    // a code
    }
Igloo answered 26/10, 2015 at 13:59 Comment(5)
I have added: public ApplicationSettingsFragment() { super(); } but the problem is the same.Galarza
just try to add this constructor, it's mentionated in android developers, just check the link in your descriptionIgloo
But I can't add your code, because it is not my class'es constructor. I have added a default constructor to my class, and called super(), because this.super() neither allowed there. But where do you find that information on the link?Galarza
Yes. As you see in the description, 23.1.0 :)Galarza
that's really strange !! plz tell me what was the pblm if u find the solution !!Igloo

© 2022 - 2024 — McMap. All rights reserved.