单击列表首选项时应用崩溃

时间:2017-10-06 15:22:39

标签: android android-preferences listpreference edittextpreference

我目前正在向我的应用添加首选项/设置活动。我一直遇到问题。 preference.xml文件加载正常,但是当我尝试单击列表首选项或Edit Text首选项时,它会使用以下堆栈跟踪来崩溃活动:

10-06 11:11:20.464 16629-16629/com.example.benhouse.weatherview E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.benhouse.weatherview, PID: 16629
                                                                              android.content.res.Resources$NotFoundException: File res/drawable/dialog_background_material.xml from color state list resource ID #0x108026d
                                                                                  at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2750)
                                                                                  at android.content.res.Resources.loadColorStateList(Resources.java:2699)
                                                                                  at android.content.res.TypedArray.getColor(TypedArray.java:439)
                                                                                  at com.android.internal.policy.PhoneWindow.generateLayout(PhoneWindow.java:3777)
                                                                                  at com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:3981)
                                                                                  at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:383)
                                                                                  at com.android.internal.app.AlertController.installContent(AlertController.java:233)
                                                                                  at android.app.AlertDialog.onCreate(AlertDialog.java:423)
                                                                                  at android.app.Dialog.dispatchOnCreate(Dialog.java:394)
                                                                                  at android.app.Dialog.show(Dialog.java:295)
                                                                                  at android.preference.DialogPreference.showDialog(DialogPreference.java:319)
                                                                                  at android.preference.DialogPreference.onClick(DialogPreference.java:277)
                                                                                  at android.preference.Preference.performClick(Preference.java:994)
                                                                                  at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:214)
                                                                                  at android.widget.AdapterView.performItemClick(AdapterView.java:310)
                                                                                  at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
                                                                                  at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
                                                                                  at android.widget.AbsListView$3.run(AbsListView.java:3879)
                                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                               Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid color state list tag inset
                                                                                  at android.content.res.ColorStateList.createFromXmlInner(ColorStateList.java:217)
                                                                                  at android.content.res.ColorStateList.createFromXml(ColorStateList.java:201)
                                                                                  at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2746)
                                                                                  at android.content.res.Resources.loadColorStateList(Resources.java:2699) 
                                                                                  at android.content.res.TypedArray.getColor(TypedArray.java:439) 
                                                                                  at com.android.internal.policy.PhoneWindow.generateLayout(PhoneWindow.java:3777) 
                                                                                  at com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:3981) 
                                                                                  at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:383) 
                                                                                  at com.android.internal.app.AlertController.installContent(AlertController.java:233) 
                                                                                  at android.app.AlertDialog.onCreate(AlertDialog.java:423) 
                                                                                  at android.app.Dialog.dispatchOnCreate(Dialog.java:394) 
                                                                                  at android.app.Dialog.show(Dialog.java:295) 
                                                                                  at android.preference.DialogPreference.showDialog(DialogPreference.java:319) 
                                                                                  at android.preference.DialogPreference.onClick(DialogPreference.java:277) 
                                                                                  at android.preference.Preference.performClick(Preference.java:994) 
                                                                                  at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:214) 
                                                                                  at android.widget.AdapterView.performItemClick(AdapterView.java:310) 
                                                                                  at android.widget.AbsListView.performItemClick(AbsListView.java:1145) 
                                                                                  at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042) 
                                                                                  at android.widget.AbsListView$3.run(AbsListView.java:3879) 
                                                                                  at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                  at android.os.Looper.loop(Looper.java:148) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

看起来崩溃是由dialog_background_material.xml没有出现引起的,但是看了无数的例子没有提到需要添加这样的文件所以我认为它是sdk或gradle应该添加的文件。

这是我的Gradle文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
    applicationId "com.example.benhouse.weatherview"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    vectorDrawables.useSupportLibrary = true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', 
{
    exclude group: 'com.android.support', module: 'support-annotations'
  })

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'com.android.support:support-v4:25.0.0'
compile 'com.android.support:design:25.3.1'
compile 'com.jsibbold:zoomage:1.1.0'
compile 'com.android.support:support-vector-drawable:25.3.1'
testCompile 'junit:junit:4.12'
}

我的prefFragment

import android.os.Bundle;
import android.preference.PreferenceFragment;

public class PrefsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preference);
    }

} 

preference.xml

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


    <PreferenceCategory
        android:title="PreferenceCategory A">

        <CheckBoxPreference
            android:key="checkbox_preference"
            android:title="title_checkbox_preference"
            android:summary="summary_checkbox_preference" />

    </PreferenceCategory>

    <PreferenceCategory
        android:title="PreferenceCategory B">

        <EditTextPreference
            android:key="edittext_preference"
            android:title="title_edittext_preference"
            android:summary="summary_edittext_preference"
            android:dialogTitle="dialog_title_edittext_preference" />

    </PreferenceCategory>

    <PreferenceCategory
        android:title="ListPreference">

        <ListPreference
            android:key="list_preference"
            android:title="title_list_preference"
            android:summary="summary_list_preference"
            android:entries="@array/listDisplayWord"
            android:entryValues="@array/listReturnValue"
            android:dialogTitle="dialog_title_list_preference" />

    </PreferenceCategory>
    </PreferenceScreen>

我真的很难过,因为我查看了很多例子和问题,没有提到这样的问题,并使用类似的代码让屏幕正常工作。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,并通过切换到Android支持库中的android.support.v14.preference.PreferenceFragment解决了该问题。请注意,您可能还需要将其他类更改为其支持库变体。