RingtonePreference defaultValue InflateException
Posted
技术标签:
【中文标题】RingtonePreference defaultValue InflateException【英文标题】: 【发布时间】:2012-06-10 22:42:38 【问题描述】:我使用 RingtonePreference 并希望使用默认通知声音作为默认偏好值。为了实现这一点,我遵循了以下建议:RingtonePreference Default value
可悲的是,实际上我得到了一个我无法以任何方式摆脱的InflateException
。是安卓、我的手机有问题还是我做错了什么?
MyPreferenceActivity
类中我的OnCreate
方法:
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
我preferences.xml
的相关片段:
<RingtonePreference
android:key="ringtone_preference"
android:ringtoneType="notification"
android:showDefault="true"
android:showSilent="true"
android:defaultValue="content://settings/system/notification_sound"
android:title="Notification sound"
android:summary=""
/>
异常的LogCat:
06-06 22:44:07.697: E/ActivityThread(21031): Failed to inflate
06-06 22:44:07.697: E/ActivityThread(21031): android.view.InflateException: Binary XML file line #17: Error inflating class java.lang.reflect.Constructor
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.GenericInflater.createItem(GenericInflater.java:397)
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.GenericInflater.inflate(GenericInflater.java:326)
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:269)
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1457)
06-06 22:44:07.697: E/ActivityThread(21031): at com.stanwise.timespacereminder.TSRPreferenceActivity.onCreate(TSRPreferenceActivity.java:44)
06-06 22:44:07.697: E/ActivityThread(21031): at android.app.Activity.performCreate(Activity.java:4465)
06-06 22:44:07.697: E/ActivityThread(21031): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-06 22:44:07.697: E/ActivityThread(21031): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
06-06 22:44:07.697: E/ActivityThread(21031): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
06-06 22:44:07.697: E/ActivityThread(21031): at android.app.ActivityThread.access$600(ActivityThread.java:133)
06-06 22:44:07.697: E/ActivityThread(21031): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
06-06 22:44:07.697: E/ActivityThread(21031): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 22:44:07.697: E/ActivityThread(21031): at android.os.Looper.loop(Looper.java:137)
06-06 22:44:07.697: E/ActivityThread(21031): at android.app.ActivityThread.main(ActivityThread.java:4586)
06-06 22:44:07.697: E/ActivityThread(21031): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 22:44:07.697: E/ActivityThread(21031): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 22:44:07.697: E/ActivityThread(21031): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-06 22:44:07.697: E/ActivityThread(21031): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-06 22:44:07.697: E/ActivityThread(21031): at dalvik.system.NativeStart.main(Native Method)
06-06 22:44:07.697: E/ActivityThread(21031): Caused by: java.lang.reflect.InvocationTargetException
06-06 22:44:07.697: E/ActivityThread(21031): at java.lang.reflect.Constructor.constructNative(Native Method)
06-06 22:44:07.697: E/ActivityThread(21031): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.GenericInflater.createItem(GenericInflater.java:383)
06-06 22:44:07.697: E/ActivityThread(21031): ... 23 more
06-06 22:44:07.697: E/ActivityThread(21031): Caused by: java.lang.NullPointerException
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.MultiSelectListPreference.onGetDefaultValue(MultiSelectListPreference.java:211)
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.Preference.<init>(Preference.java:260)
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.DialogPreference.<init>(DialogPreference.java:69)
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.DialogPreference.<init>(DialogPreference.java:90)
06-06 22:44:07.697: E/ActivityThread(21031): at android.preference.MultiSelectListPreference.<init>(MultiSelectListPreference.java:49)
06-06 22:44:07.697: E/ActivityThread(21031): ... 26 more
我还尝试插入字符串DEFAULT_NOTIFICATION_URI
而不是content://settings/system/notification_sound
,但没有任何成功。完全删除 android:defaultValue
字段后,一切正常,除了我偏好的默认值是静音铃声而不是默认通知声音,这对我来说是不可接受的。
我到处搜索,找不到解决这个问题的方法。如果没有其他方法,我将使用此处发布的 hack:https://***.com/a/4298638/1265432,但我想妥善解决这个问题。
编辑:问题存在于我的 CM9 Motorola Defy 和 4.0.4 (API 15) 和 2.1 (API 7) 的模拟器中,所以我认为手机不是问题。 Bug,还是我做错了什么?
【问题讨论】:
如果你记录Settings.System.DEFAULT_NOTIFICATION_URI
常量的值,那么它和你预期的一样吗?如果是,那么它看起来像一个 Android 错误。如果不是,那么它只是将其值从设备/API 更改为设备/API,然后唯一的方法就是以编程方式设置它。
是的,Settings.System.DEFAULT_NOTIFICATION_URI == "content://settings/system/notification_sound"
【参考方案1】:
我在这里提交了 Android 的错误报告:http://code.google.com/p/android/issues/detail?id=33220
与此同时,我使用以下解决方法,我发现它比问题中链接的方法更简洁。我将MyPreferenceActivity
的OnCreate
方法修改为:
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
Uri notificationSound = Uri.parse(PreferenceManager.getDefaultSharedPreferences(this)
.getString("ringtone_preference", System.DEFAULT_NOTIFICATION_URI.toString()));
if (notificationSound.equals(System.DEFAULT_NOTIFICATION_URI))
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putString("ringtone_preference", System.DEFAULT_NOTIFICATION_URI.toString())
.commit();
addPreferencesFromResource(R.xml.preferences);
【讨论】:
【参考方案2】:我对您的错误报告发表了评论,对问题的真正含义有所了解。
无论如何,这里有一个对我有用的更简单的解决方法:
为您的偏好 XML 中 RingtonePreference 之后的 MultiSelectListPreference 添加一个 defaultValue:
<MultiSelectListPreference
...
android:defaultValue="@array/empty"
...
/>
在你的 strings.xml 中:
<string-array name="empty"/>
【讨论】:
以上是关于RingtonePreference defaultValue InflateException的主要内容,如果未能解决你的问题,请参考以下文章
if-not-none-then-none-else-default 的简短声明 [重复]
键盘iPhone-Portrait-NumberPad找不到支持类型4的键盘;使用3876877096_Portrait_iPhone-Simple-Pad_Default