Bug 1359 - PreferenceActivity Headers
Summary: PreferenceActivity Headers
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 1.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2011-10-08 05:20 UTC by andi
Modified: 2011-11-04 12:44 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 andi 2011-10-08 05:20:56 UTC
When using header resources for preference activity an java.lang.NullPointerException exception is raised when base.OnCreate(...) is called. Version 1.9.1.r2-2

Preferences header:

<?xml version="1.0" encoding="utf-8"?>
  <header android:icon="@drawable/logo"
    <intent android:action="android.intent.action.VIEW"
            android:data="http://android.xamarin.com" />

Stack Trace:

  at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00024] in /home/jpobst/Desktop/monodroid/Mono.Android/src/Runtime/JNIEnv.cs:793 
  at Android.App.Activity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00070] in /home/jpobst/Desktop/monodroid/Mono.Android/platforms/android-12/src/generated/Android.App.Activity.cs:1784 
  at myapp.android.activities.CommonPreferencesActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00002] in Y:\development\myapp\source.android\myapp.android\myapp.android\activities\CommonPreferencesActivity.cs:38 
  --- End of managed exception stack trace ---
	at java.lang.VMClassLoader.findLoadedClass(Native Method)
	at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:530)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:500)
	at android.app.Fragment.instantiate(Fragment.java:490)
	at android.preference.PreferenceActivity.switchToHeaderInner(PreferenceActivity.java:1048)
	at android.preference.PreferenceActivity.switchToHeader(PreferenceActivity.java:1081)
	at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:531)
	at myapp.android.activities.CommonPreferencesActivity.n_onCreate(Native Method)
	at myapp.android.activities.CommonPreferencesActivity.onCreate(CommonPreferencesActivity.java:26)
	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
	at android.app.ActivityThread.access$1500(ActivityThread.java:122)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loop(Looper.java:132)
	at android.app.ActivityThread.main(ActivityThread.java:4025)
	at java.lang.reflect.Method.invokeNative(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:491)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
	at dalvik.system.NativeStart.main(Native Method)
Comment 1 Atsushi Eno 2011-11-04 07:37:31 UTC
I have created a working sample for preference-headers under monodroid-samples.

(To try the sample, please also check the commit message that contains how to "enable" the test; it is disabled for some reason.)

My guess is, your preference_headers.xml lacks some mandatory contents.

Preference headers resources is somewhat messed in Mono for Android land. It specifies Java class name for the PreferenceFragment. In the example port above, I had to rename some Java names in the xml, to replace nested classes (foo$bar) to non-nested name (foo_bar). It is what Mono for Android internally does and needs some special attention if you use nested types like the example above.

If you still think your sample should work, tell us and we'll investigate.
Comment 2 Jonathan Pryor 2011-11-04 12:44:28 UTC
andi: We need a more complete example to reproduce the issue (e.g. an attached project).

When I use the XML you provided, placed into Resources\xml\preference_headers.xml, add a Resources\drawable\logo.png, and provide the following activity:

	using System;
	using Android.App;
	using Android.Content;
	using Android.Runtime;
	using Android.Views;
	using Android.Preferences;
	using Android.Widget;
	using Android.OS;
	namespace Scratch.PreferenceHeaders
		[Activity (Label = "Scratch.PreferenceHeaders", MainLauncher = true)]
		public class Activity1 : PreferenceActivity
			int count = 1;
			protected override void OnCreate (Bundle bundle)
				base.OnCreate (bundle);
The activity loads w/o error. Nothing is displayed, but there is no crash.