Search code examples
androiddefault-valueringtone

RingtonePreference defaultValue InflateException


I use RingtonePreference and would like to use default notification sound as the default preference value. To achieve this I have followed advice in: RingtonePreference Default value

Sadly, in effect I got an InflateException that I cannot get rid of in any way. Is it a bug in android, my phone or am I doing something wrong?

My OnCreate method in MyPreferenceActivity class:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
}

Relevant fragment of my 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 of the exception:

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

I also tried inserting a string DEFAULT_NOTIFICATION_URI instead of content://settings/system/notification_sound, but without any success. After removing the android:defaultValue field completely, everything works fine apart from the fact, that default value of my preference is Silent ringtone instaed of the default notification sound, which is unacceptable for me.

I have searched everywhere and cannot find a solution to this problem. If nothing else works, I will use a hack posted here: https://stackoverflow.com/a/4298638/1265432, but I would like to solve this properly.

EDIT: Problem exists on both my CM9 Motorola Defy and in Emulators of 4.0.4 (API 15) and 2.1 (API 7), so I think the phone is not a problem. Bug, or am I doing something wrong?


Solution

  • I filed a bug report for Android here: http://code.google.com/p/android/issues/detail?id=33220

    In the meantime, I use the following workaround, which I found cleaner than the one linked in the question. I modified OnCreate method of MyPreferenceActivity to this:

    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);
    }