Bug 4416 - App fails after calling FindPreference in PreferenceActivity
Summary: App fails after calling FindPreference in PreferenceActivity
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.1.x
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-04-13 04:05 UTC by honza.keller
Modified: 2012-04-19 11:15 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description honza.keller 2012-04-13 04:05:53 UTC
When running on Android 2.x, the exception occurs when I try to call FindPreference in PreferenceActivity. The target preference is of type CheckBoxPreference.

Target Framework set to Android 4.0

System.TypeInitializationException: An exception was thrown by the type initializer for Android.Preferences.TwoStatePreference ---> Java.Lang.NoClassDefFoundError: Exception of type 'Java.Lang.NoClassDefFoundError'
Comment 1 Atsushi Eno 2012-04-19 02:45:28 UTC
This is because TwoStatePreference exists only after API Level 14.

There is no way to use in in Android 2.x. It also applies to Java-based application.
Comment 2 honza.keller 2012-04-19 03:07:10 UTC
I know that it exists in API Level 14 and later but the problem is, that I haven't use TwoStatePreference at all. I have classic CheckBoxPreference in my XML. It worked fine this way in Mono for Android 4.0.x.
Comment 3 Jonathan Pryor 2012-04-19 09:17:12 UTC
The problem is that API level 14 changed the base class of CheckBoxPreference to TwoStatePreference; previously it's base class was Preference.

Furthermore, in Mono for Android < 4.1.1 we lookup the corresponding classes in a static fashion within the static constructor:

    // http://docs.xamarin.com/android/advanced_topics/Binding_Android_Types#Declaring_Type_Handle
    public partial class TwoStatePreference : Preference {
        static IntPtr class_ref = JNIEnv.FindClass("android/preference/TwoStatePreference");
    public partial class CheckBoxPreference : TwoStatePreference {
        static IntPtr class_ref = JNIEnv.FindClass("android/preference/CheckBoxPreference");

Consequently, when you try to use your API 14-targeted app on a < API 14 device we'll try to lookup the TwoStatePreference type, which fails, resulting in the NoClassDefFoundError that you reported.

This is fixed in Mono for Android 4.1.1, though you still need to ensure that you don't actually use any types/members that don't exist on the device's supported API level (e.g. don't invoke TwoStatePreference.OnClick()).

In the meantime, you should set your TargetFrameworkVersion to the _minimum_ API level that you want to target -- if you want to run on at least API level 8 (Android v2.2), then you should build against API level 8, _not_ API level 14.
Comment 4 honza.keller 2012-04-19 09:36:21 UTC
Thank you for explanation. In my app I need at least API 12, because i want support Android 4 with Holo Theme and ActionBar. Fortunately I need nothing from API 14 so far.
Comment 5 Jonathan Pryor 2012-04-19 11:01:02 UTC
ActionBar doesn't exist on Android v2.x either, unless you're ensuring that ActionBar is only used on Android v3.x devices and later. If you want to use ActionBar, if possible you should use the 4.1.0 alpha and use the Compatibility assemblies:


Note that 4.1.1 renames the Compatibility assemblies to Support assemblies.
Comment 6 honza.keller 2012-04-19 11:15:52 UTC
I know that but thank you anyway. For that purpose I have rewrite ActionBarCompat from http://developer.android.com/resources/samples/ActionBarCompat/index.html to support ActionBar on Android 2x. I'm already using fragments and ViewPager from 4.1.0. Everything works fine. Only issue was the first I reported.