简体   繁体   中英

App Crashing when clicking on list preference

I am currently in the process of adding a Preferences/Settings activity to my app. I keep running into a problem though. The preference.xml file loads fine but as soon as I try to click either a list preference or an Edit Text preference it crashes the activity with the following stack trace:

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) 

It looks like the crash is being caused by dialog_background_material.xml not being present but looking through countless examples none mention needing to add such a file so I presume it is a file either the sdk or gradle is supposed to add.

Here is my Gradle file:

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'
}

And my 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>

I am really stumped on this as I have looked through so many examples and questions and none mention such an issue and use similar code to get their screen working.

I had the same issue and solved it by switching to android.support.v14.preference.PreferenceFragment in the Android support library. Note that you might need to change other classes to their support library variant as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM