[英]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: 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)
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. 看起来崩溃是由不存在
dialog_background_material.xml
引起的,但通过无数示例来看,没有提到需要添加这样的文件,因此我认为它是应该添加sdk或gradle的文件。
Here is my Gradle file: 这是我的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'
}
And my prefFragment
: 和我的
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
: 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. 我遇到了同样的问题,并通过切换到Android支持库中的
android.support.v14.preference.PreferenceFragment
解决了该问题。 Note that you might need to change other classes to their support library variant as well. 请注意,您可能还需要将其他类更改为其支持库变体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.