PreferenceFragmentCompat 在初始化时崩溃

PreferenceFragmentCompat crashes on initalization

我用 ViewPager 创建了一个应用程序。 当我进行调试构建并在我的 phone 上进行测试时,一切正常。 (不是模拟器) 但是当我构建一个 release .apk 并安装它时,PreferenceFragmentCompat 的 addPreferencesFromResource(R.xml.preferences); 使应用程序崩溃。 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>

以及崩溃报告:

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) 

我使用的是最新的库:

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

有人可以告诉我我做错了什么吗?谢谢

尝试在您的 java 代码中添加此构造函数:

    PreferenceFragmentCompat(){
this.super();
    // a code
    }

因为你导入了android.support.v7.preference.PreferenceFragmentCompat,所以你需要<android.support.v7.preference.PreferenceScreen .../><android.support.v7.preference.CheckBoxPreference .../>。它看起来像这样:

<?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>

有关这些的更多信息 new preference types v7 支持库支持。

编辑

而不是重写默认的 onCreate(Bundle) 方法,但你应该只重写 onCreatePreferences(Bundle, String):

@Override
public void onCreatePreferences(Bundle bundle, String s) {
    addPreferencesFromResource(R.xml.preferences);
}

OP 说他在构建 APK 文件时使用了 ProGuard。

this 错误报告所述

Make sure your proguard configuration contains a line like this:

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

这可能是问题所在,虽然我不确定。

修复了最新 androidx 的所有混淆器问题

-keep public class * extends androidx.preference.* { *;}