PreferenceActivity nougat无法正常工作

时间:2017-04-17 12:46:03

标签: android sharedpreferences preferenceactivity android-7.0-nougat

我在android中使用了以下代码,但它在android模拟器中不起作用。

给出以下错误。

      --------- beginning of crash
04-17 17:53:48.076 4802-4802/com.package E/AndroidRuntime: FATAL EXCEPTION: main
                                                              Process: com.package, PID: 4802
                                                              android.view.InflateException: Binary XML file line #30: Error inflating class <unknown>
                                                                  at android.view.LayoutInflater.createView(LayoutInflater.java:633)
                                                                  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
                                                                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                  at android.preference.Preference.onCreateView(Preference.java:510)
                                                                  at android.preference.Preference.getView(Preference.java:487)
                                                                  at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:246)
                                                                  at android.widget.AbsListView.obtainView(AbsListView.java:2347)
                                                                  at android.widget.ListView.makeAndAddView(ListView.java:1864)
                                                                  at android.widget.ListView.fillDown(ListView.java:698)
                                                                  at android.widget.ListView.fillFromTop(ListView.java:759)
                                                                  at android.widget.ListView.layoutChildren(ListView.java:1673)
                                                                  at android.widget.AbsListView.onLayout(AbsListView.java:2151)
                                                                  at android.view.View.layout(View.java:15671)
                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5038)
                                                                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                  at android.view.View.layout(View.java:15671)
                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5038)
                                                                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                  at android.view.View.layout(View.java:15671)
                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5038)
                                                                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                  at android.view.View.layout(View.java:15671)
                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5038)
                                                                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                  at android.view.View.layout(View.java:15671)
                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5038)
                                                                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                  at android.view.View.layout(View.java:15671)
                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5038)
                                                                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                  at android.view.View.layout(View.java:15671)
                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5038)
                                                                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                  at android.view.View.layout(View.java:15671)
                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5038)
                                                                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                  at android.view.View.layout(View.java:15671)
                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5038)
                                                                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                  at android.view.View.layout(View.java:15671)
                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5038)
                                                                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                  at android.view.View.layout(View.java:15671)
                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5038)
                                                                  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
                                                                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
                                                                at android.view.ViewRootImpl
04-17 17:53:48.077 1965-2042/system_process W/ActivityManager:   Force finishing activity 1 com.package/.MyTabActivity

以下是我使用的代码。

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.util.Log;

import com.google.android.gms.analytics.GoogleAnalytics;

import org.jraf.android.backport.switchwidget.SwitchPreference;

public class SettingsActivity extends PreferenceActivity {
    private static final String TAG = "SettingsActivity";

    SharedPreferences mPrefs;

    SharedPreferences.Editor editor;

    PreferenceCategory prefCategoryGenerals;

    ListPreference pitchLevel, recordingType, visualizerType;

    SwitchPreference  showQuestionTextSwitch, visualizerSwitch;

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

        //addPreferencesFromResource(R.xml.settings);
        //getSherlock().getActionBar().setDisplayHomeAsUpEnabled(true);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();

        //mPrefs = getSharedPreferences(Util.PREFS_SETTINGS, MODE_PRIVATE);
        //editor = mPrefs.edit();

    }
    public static class MyPreferenceFragment extends PreferenceFragment
    {
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.settings);
        }
    }
    @Override
    protected void onResume() {
        //init();
        super.onResume();
    }
    @Override
    protected void onStart() {
        //EasyTracker.getInstance().activityStart(this); // Add this method.
        GoogleAnalytics.getInstance(this).reportActivityStart(this);
        super.onStart();
    }
    @Override
    public void onStop() {
        super.onStop();
        // The rest of your onStop() code.
        //EasyTracker.getInstance().activityStop(this); // Add this method.
        GoogleAnalytics.getInstance(this).reportActivityStop(this);
    }
    Preference.OnPreferenceChangeListener changeListener = new Preference.OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (newValue instanceof String) {
                editor.putString(preference.getKey(), (String)newValue);
                editor.commit();
            } else if (newValue instanceof Boolean) {
                editor.putBoolean(preference.getKey(), (Boolean)newValue);
                editor.commit();
            }
            Log.e(preference.getKey(),newValue.toString());
            updatePreferences();
            return true;
        }
    };

    private void init() {
        prefCategoryGenerals = (PreferenceCategory)findPreference(getString(R.string.settings_general));

        pitchLevel = (ListPreference)findPreference(getString(R.string.pitch_duration));
        pitchLevel.setOnPreferenceChangeListener(changeListener);

        recordingType = (ListPreference)findPreference(getString(R.string.recording_type));
        recordingType.setOnPreferenceChangeListener(changeListener);

        visualizerSwitch = (SwitchPreference)findPreference(getString(R.string.visualizer));
        visualizerSwitch.setOnPreferenceChangeListener(changeListener);

        showQuestionTextSwitch = (SwitchPreference)findPreference(getString(R.string.question_text));
        showQuestionTextSwitch.setOnPreferenceChangeListener(changeListener);

        visualizerType = (ListPreference)findPreference(getString(R.string.visualizer_type));
        visualizerType.setOnPreferenceChangeListener(changeListener);

        updatePreferences();

    }

    private void updatePreferences() {
        boolean recordingTypeVideo = mPrefs.getString(Util.PREFS_RECORDING_TYPE, Util.PREFS_RECORDING_A).equals(Util.PREFS_RECORDING_V);
        boolean visualizerEnabled = mPrefs.getBoolean(Util.PREFS_SHOW_VISUALIZER, false);
        boolean visualizerTypeEnabled = !recordingTypeVideo && visualizerEnabled;
        visualizerType.setEnabled(visualizerTypeEnabled);
        visualizerSwitch.setEnabled(!recordingTypeVideo);
    }

}

以下是Settings.xml文件的代码。

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

    <PreferenceCategory
        android:key="@string/settings_general"
        android:title="@string/settings_general" >
        <ListPreference
            android:key="@string/pitch_duration"
            android:title="@string/pitch_duration"
            android:defaultValue="@string/pitch_standard"
            android:entries="@array/pitch"
            android:entryValues="@array/pitch"
            android:summary="@string/summary_pitch_duration"
            />
        <ListPreference
            android:key="@string/recording_type"
            android:title="@string/recording_type"
            android:defaultValue="@string/audio_recording"
            android:entries="@array/recording_type"
            android:entryValues="@array/recording_type"
            android:summary="@string/summary_recording_type"
             />

        <org.jraf.android.backport.switchwidget.SwitchPreference
            android:key="@string/question_text"
            android:title="@string/question_text"
            switchpref:asb_summaryOff="@string/switch_prefs_question_text"
            switchpref:asb_summaryOn="@string/switch_prefs_question_text"
            switchpref:asb_switchTextOff="@string/off"
            switchpref:asb_switchTextOn="@string/on"/>

        <org.jraf.android.backport.switchwidget.SwitchPreference
            android:key="@string/visualizer"
            android:title="@string/visualizer"
            android:defaultValue="false"
            switchpref:asb_summaryOff="@string/switch_prefs_visualizer"
            switchpref:asb_summaryOn="@string/switch_prefs_visualizer"
            switchpref:asb_switchTextOff="@string/off"
            switchpref:asb_switchTextOn="@string/on" />

        <ListPreference
            android:key="@string/visualizer_type"
            android:title="@string/visualizer_type"
            android:defaultValue="@string/visualizer_line"
            android:entries="@array/visualizer_type"
            android:entryValues="@array/visualizer_type"
            android:summary="@string/summary_visualizer_type"
             />
        <com.iq.android.preferences.AboutDialogPref
            android:dialogLayout="@layout/dialog_about_layout"
            android:key="funThing"
            android:negativeButtonText="Cancel"
            android:positiveButtonText="OK"
            android:title="About" />
    </PreferenceCategory>

</PreferenceScreen>

以上整个代码在android 4.4.4仿真器和设备中也能正常工作。

1 个答案:

答案 0 :(得分:0)

最后我想出了我们的问题。

以下库中存在问题。这不适用于PreferenceFragment。

org.jraf:android-switch-backport:2.0.1

所以我实施了SwitchPreference而不是这个。