PreferenceFragmentCompat将不会加载到AppCompatPreferenceActivity中 - 尝试实例化一个不是片段的类

Ben*_*654 5 android android-appcompat android-preferences android-fragments android-support-library

我正在尝试使用v7支持库中的PreferenceFragmentCompat.当我尝试添加它时,它总是返回以下异常

Process: com.sample.preferencetest, PID: 14444
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.preferencetest/com.sample.preferencetest.SettingsActivity}: android.app.Fragment$InstantiationException: Trying to instantiate a class com.sample.preferencetest.SettingsActivity$EmptyFragment that is not a Fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3155)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3263)
at android.app.ActivityThread.access$1000(ActivityThread.java:197)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6897)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Run Code Online (Sandbox Code Playgroud)

当我尝试使用PreferenceFragment时,一切正常.只有当我尝试使用它失败的PreferenceFragmentCompat时

我想使用它的原因是因为现在不推荐使用onAttach(Activity)方法,而早期的设备没有附加我的界面.

这是班级

package com.sample.preferencetest;

import android.os.Bundle;
//import android.preference.PreferenceFragment;  I toggle this to try regular preference fragments.
import android.support.v7.preference.PreferenceFragmentCompat;
import java.util.List;

public class SettingsActivity extends AppCompatPreferenceActivity {

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

    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.pref_headers, target);
    }

    protected boolean isValidFragment(String fragmentName) {
        return true;
    }

    public static class EmptyFragment extends PreferenceFragmentCompat {
        public EmptyFragment() {
        }

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

        @Override
        public void onCreatePreferences(Bundle bundle, String s) {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

标头XML

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
    <header
    android:fragment="com.sample.preferencetest.SettingsActivity$EmptyFragment"
    android:icon="@drawable/ic_info_black_24dp"
    android:title="@string/pref_header_general" />
</preference-headers>
Run Code Online (Sandbox Code Playgroud)

摇篮

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.sample.preferencetest"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile 'com.android.support:support-v4:23.2.1'
    compile 'com.android.support:preference-v7:23.3.0'
}
Run Code Online (Sandbox Code Playgroud)

我怎么能使用PreferenceFragmentCompat,如果不是这样的话?

tyn*_*ynn 7

出现此问题,因为框架片段和支持片段不兼容.如果您使用AppCompatPreferenceActivity支持库的示例,则使用PreferenceActivity工具栏支持的常规.那里不支持支持片段.这就是为什么PreferenceFragment工作正常而PreferenceFragmentCompat不是.

如果您只关心onAttach(Activity)被弃用,请不要.默认实现onAttach(Context)只是在附加到活动时调用它.因此,只有实现onAttach(Activity)与所有当前版本的Android兼容才是安全的.