我使用 RingtonePreference 并希望使用默认通知声音作为默认首选项值。为此,我遵循了以下建议:RingtonePreference 默认值
可悲的是,实际上我得到了一个InflateException
我无法以任何方式摆脱的东西。这是android,我的手机中的错误还是我做错了什么?
我OnCreate
在MyPreferenceActivity
课堂上的方法:
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 ://stackoverflow.com/a/4298638/1265432 ,但我想妥善解决这个问题。
编辑:我的 CM9 Motorola Defy 和 4.0.4(API 15)和 2.1(API 7)的模拟器都存在问题,所以我认为手机不是问题。错误,还是我做错了什么?