PreferenceFragmentCompat在初始化时崩溃

时间:2015-10-26 13:33:09

标签: android android-support-library preferencefragment preference-v7

我使用ViewPager创建了一个应用程序。 当我进行调试构建并在手机上测试时,一切正常。 (不是模拟器) 但是当我构建一个发行版.apk并安装它时,PreferenceFragmentCompat的 addPreferencesFromResource(R.xml.preferences); 会使应用程序崩溃。 enter image description here

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'

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

4 个答案:

答案 0 :(得分:4)

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

正如this错误报告所述

  

确保您的proguard配置包含如下所示的行:

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

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

答案 1 :(得分:3)

由于您导入了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>

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

修改

而不是覆盖默认的onCreate(Bundle)方法,但您只应覆盖onCreatePreferences(Bundle, String)

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

答案 2 :(得分:0)

修复了最新androidx的所有proguard问题

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

答案 3 :(得分:-1)

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

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