In my ongoing quest to write about little known or poorly documented features of Android, I stumbled across something this morning that I feel needs a little bit more exposure, as it frustrated me for quite a while trying to figure it out.

The set up: I was trying to include into my application a ListPreference object, which basically just creates a dialog with a list of selectable items in it for use with a PreferenceActivity. Quite handy, particularly if you need to let the user select from more than one option, say, for an update timer or similar.

So, I set up my preferences page thusly:




Nothing fancy here, except to note that the ListPreference takes a couple additional arguments, namely entries, which is an array of human readable values, and entryValues, which is an array of data values that each correspond to the values in entries.

Next, I set up a PreferenceActivity to create my preferences page:

public class Prefs extends PreferenceActivity {

	private static final String OPT_TIMER = "set_timer";
	private static final String OPT_TIMER_DEF = "5";

	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);
	}

	public static String getTimer(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context)
		.getString(OPT_TIMER, OPT_TIMER_DEF);
	}

}

Okay, at this point, we should be all set up to start using our preferences…or at least we would be if there weren’t a bit of bug with the current release of Android. If you were to run this code right now, with your two arrays, entries and entryValues set up in a resource file, chances are you would get a null pointer exception. How can that be possible though, since technically, nothing we’ve written is even doing anything? Its all relying on the underlying Android framework at this point…

Well, as it turns out, we can track this issue back the arrays we are using. When I first set up my preferences page, I built two arrays that looked like the following:


	
		1 minute
		2 minutes
		5 minute
		10 minutes
	
	
		1
		2
		5
		10
	

As you can see, one is an array of String, while the other is an array of int. This is where the bug lies. The ListPreference object does not support type mismatches on the two arrays it needs. If we were to change the above code to:


	
		1 minute
		2 minutes
		5 minute
		10 minutes
	
	
		1
		2
		5
		10
	

...Where both arrays are of type String, the null pointer exception goes away, and everything chugs along just fine. This really is only a minor, if not perplexing bug, as it is not a big deal to cast a string into an int. But, as I stated earlier, I felt it was worth bringing up, if only to save some fellow programmers some of the headaches I endured trying to figure it out.

The official bug report can be found here.

You may also be interested in these articles:

  1. Working with App widgets – Android
  2. Creating directories on the SD card – Android
  3. ProgressDialog and You: The Basics – Android