Forum Xamarin.Android

NullReferenceException on SetContentView () after launching camera intent

Jean-LouisKroonJean-LouisKroon ZAMember
edited October 2012 in Xamarin.Android

I'm getting very strange behavior only on HTC devices. My application works a 100% on all other devices except on HTC devices.

The issue happens after I have launched the camera intent and I pop back to previous layouts and then try to navigate back to the activity that launched the camera intent. I get a null reference error in the setContentView() method in OnCreate().

I have included my LaunchCameraIntent and OnActivityResult() functions.

Anyone have an idea?

I have read on another thread that it could be an HTC specific issue that occurs when an intent is launched from an activity and that for some reason the Garbage Collector is called to clear that activity and thus when trying to navigate back this results in a nullreference error.

Here is the thread

protected override void OnCreate (Bundle bundle)
   base.OnCreate (bundle);
   this.SetContentView (Resource.Layout.PhotoAccidentCarLayout);

private void LaunchCameraIntent (int picType)
        Intent intent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
        MDCarPicStatus.PicUri = ContentResolver.Insert (isMounted ? Android.Provider.MediaStore.Images.Media.ExternalContentUri : Android.Provider.MediaStore.Images.Media.InternalContentUri, new ContentValues ());
        intent.PutExtra (Android.Provider.MediaStore.ExtraOutput, MDCarPicStatus.PicUri);
        intent.PutExtra ("isCamera", true);
        MDCameraHelper.isCamera = true;
        StartActivityForResult (intent, picType);

protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)

        if ((resultCode == Result.Ok)) {
            if (data == null) {
                SaveImage (MDCarPicStatus.PicUri, (PhotoType)requestCode);
            } else {
                if (data.Data == null) {
                    SaveImage (MDCarPicStatus.PicUri, (PhotoType)requestCode);
                } else {
                    SaveImage (data.Data, (PhotoType)requestCode);

        base.OnActivityResult (requestCode, resultCode, data);


this is my stack trace

System.NullReferenceException: Object reference not set to an instance of an object
  at (wrapper delegate-invoke) <Module>.invoke_void__this___intptr_intptr_intptr_intptr_JValue[] (intptr,intptr,intptr,intptr,Android.Runtime.JValue[]) <IL 0x00062, 0x00153>
  at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00000] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.6-branch/632e6ddf/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:612
  at Android.App.Activity.SetContentView (int) [0x0006b] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.6-branch/632e6ddf/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.App.Activity.cs:3119
  at CCIC.MD.PhotoAccidentCarActivity.OnCreate (Android.OS.Bundle) [0x00009] in c:\Work\Sepia\Source\\CCIC\trunk\CCIC.MD\CCIC.MD\Activities\Claim\PhotoAccidentCarActivity.cs:41
  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.6-branch/632e6ddf/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.App.Activity.cs:1490
  at (wrapper dynamic-method) object.2b7db9d6-0abc-4175-b134-cda100e112d6 (intptr,intptr,intptr) <IL 0x00012, 0x00033>

I doubt that the issue lies within the xml. Unless it is an issue with the tab controller (PhotoAccidentCarLayout is embedded in a tab controller)? I have a main dashboard screen in the app that launches multiple activities (a dashboard screen). When I start the activity(lets call it dashActivity1) that itself has an activity that launches the camera intent (PhotoAccidentCarLayout) the issue pops up. After that I will get a null reference exception when trying to start any activity within dashActivity1. For a couple of seconds there is no issue launching activities... it only kicks in in about 4 seconds. STRANGE!

Anyone have any idea? Thank you.

Sign In or Register to comment.