user409849
user409849

Reputation: 71

Weird exception from PreferencesActivity

I made some edits to my preferences.xml file, nothing that generated any syntax flags in Eclipse, and now I'm getting the following weird error when I try to inflate my PreferencesActivity:

E/AndroidRuntime( 3480): java.lang.RuntimeException: Unable to start activity Co mponentInfo{com.shipmate/com.shipmate.PreferencesActivity}: java.lang.IllegalArg umentException: Cannot add a PreferenceCategory directly to a PreferenceCategory

I always have PreferenceCategory's inside other PreferenceCategory's so I don't think that's the problem. What does it mean that it thinks I'm "adding" one PreferenceCategory to another? What does "add" mean in XML? And how does an IllegalArgument relate to this addition? Any ideas on how I might isolate this problem?

Upvotes: 2

Views: 1093

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006594

I always have PreferenceCategory's inside other PreferenceCategory's so I don't think that's the problem.

By my reading of the Android source code, PreferenceCategory cannot be an immediate child of PreferenceCategory. Moreover, you should never need that, since that has no visual benefit -- have the PreferenceCategory elements be peers of each other in the XML, not nested.

What does it mean that it thinks I'm "adding" one PreferenceCategory to another?

Uh, that seems pretty self-explanatory. You have a PreferenceCategory, and you are attempting to put another PreferenceCategory inside of it. This is done, whether in code or via inflation, via an addPreference() method on PreferenceGroup. That in turn calls onPrepareAddPreference(), which in PreferenceCategory does the category-in-category check and throws your exception.

And how does an IllegalArgument relate to this addition?

It is the particular flavor of RuntimeException that the Android developers chose to use in this case. They could have chosen DivideByZeroException, for example, but that would have been rather odd.

Upvotes: 2

Related Questions