RingtonePreference defaultValue InflateException

时间:2012-06-06 21:07:20

标签: android default-value ringtone

我使用RingtonePreference,并希望使用默认通知声音作为默认首选项值。为实现这一目标,我遵循了以下建议:RingtonePreference Default value

可悲的是,实际上我得到了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字段后,除了事实之外,一切正常,我偏好的默认值是默认通知声音的静音铃声,这对我来说是不可接受的。

我到处搜索,无法找到解决此问题的方法。如果没有其他工作,我将使用这里发布的黑客:https://stackoverflow.com/a/4298638/1265432,但我想妥善解决这个问题。

编辑:我的CM9摩托罗拉Defy和4.0.4(API 15)和2.1(API 7)的仿真器都存在问题,所以我认为手机不是问题。错误,还是我做错了什么?

2 个答案:

答案 0 :(得分:0)

我在此处提交了针对Android的错误报告:http://code.google.com/p/android/issues/detail?id=33220

与此同时,我使用以下解决方法,我发现它比问题中链接的更清晰。我将OnCreate的{​​{1}}方法修改为:

MyPreferenceActivity

答案 1 :(得分:0)

我对您的错误报告发表了评论,并对问题的实际情况有所了解。

无论如何,这是一个更简单的解决方法,对我有用:

在首选项XML中为RingtonePreference添加MultiSelectListPreference的defaultValue:

<MultiSelectListPreference
        ...
        android:defaultValue="@array/empty"
        ...
        />

在你的strings.xml中:

<string-array name="empty"/>