Bug 1103 - Android.Util.AndroidRuntimeException: Exception of type 'Android.Util.AndroidRuntimeException' was thrown
Summary: Android.Util.AndroidRuntimeException: Exception of type 'Android.Util.Android...
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 1.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-09-28 11:43 UTC by Philippe Girolami
Modified: 2011-09-29 11:41 UTC (History)
2 users (show)

Tags:
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:
Status:
RESOLVED FIXED

Description Philippe Girolami 2011-09-28 11:43:10 UTC
Similar to https://bugzilla.novell.com/show_bug.cgi?id=632871 I am getting the following exception. I don't see any explanation on that ticket for the "INVALID" resolution.
Happens when running the following:
            Intent vc = new Intent(context, typeof(AnActivity));
            StartActivity(vc);

Running latest monodroid on VS2010, target version is Android 2.3

E/mono    ( 1589): Unhandled Exception: Android.Util.AndroidRuntimeException: Exception of type 'Android.Util.AndroidRuntimeException' was thrown.
E/mono    ( 1589):   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:740 
E/mono    ( 1589):   at Android.Content.ContextWrapper.StartActivity (Android.Content.Intent intent) [0x00070] in /home/jpobst/Desktop/monodroid/Mono.Android/platforms/android-8/src/generated/Android.Content.ContextWrapper.cs:2211 
E/mono    ( 1589):   at xxx.xxxApplication.RequestPage (xxx.AndroidHttpRequest request) [0x00035] in D:\TFS\Android\xxx\AppDelegate.cs:58 
E/mono    ( 1589):   at xxx.xxxApplication.RequestPage (System.String module, System.String pageName, System.String cdlName, System.Collections.Specialized.NameValueCollection query) [0x0000c] in D:\TFS\Android\xxx\AppDelegate.cs:43 
E/mono    ( 1589):   at xxx.Login.<OnCreate>b__1 () [0x0007d] in D:\TFS\Android\xxx\Login.cs:59 
E/mono    ( 1589):   at System
D/Zygote  (   82): Process 1589 exited cleanly (1)
Comment 1 Jonathan Pryor 2011-09-28 12:58:37 UTC
Odd; the exception output should be printing out the Java side stack trace as well, yet I don't see this in the output you provided. Which version is this?

For example, when I do:

    StartActivity (new Intent ("blah"));

the exception I get with 1.2.0 is:

E/mono    ( 4363): Unhandled Exception: Android.Content.ActivityNotFoundException: Exception of type 'Android.Content.ActivityNotFoundException' was thrown.
E/mono    ( 4363):   at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00000] in <filename unknown>:0 
E/mono    ( 4363):   at Android.Content.ContextWrapper.StartActivity (Android.Content.Intent intent) [0x00000] in <filename unknown>:0 
E/mono    ( 4363):   at ScratchARE.Activity1.OnCreate (Android.OS.Bundle bundle) [0x00000] in <filename unknown>:0 
E/mono    ( 4363):   at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) [0x00000] in <filename unknown>:0 
E/mono    ( 4363):   at (wrapper dynamic-method) object:02d195bb-7739-49af-94d4-d1c1ac8865d2 (intptr,intptr,intptr)
E/mono    ( 4363):   --- End of managed exception stack trace ---
E/mono    ( 4363): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=blah }
E/mono    ( 4363): 	at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
E/mono    ( 4363): 	at android.app.Inst

Regardless, you're getting an AndroidRuntimeException. One possible reason for this is frameworks/base/core/java/android/app/ContextImpl.java:

    @Override
    public void startActivity(Intent intent) {
        if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
            throw new AndroidRuntimeException(
                    "Calling startActivity() from outside of an Activity "
                    + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
                    + " Is this really what you want?");
        }
        mMainThread.getInstrumentation().execStartActivity(
            getOuterContext(), mMainThread.getApplicationThread(), null, null, intent, -1);
    }

Though another possibility is frameworks/base/core/java/android/app/Instrumentation.java in checkStartActivityResult(), in which AndroidRuntimeException is thrown as the default "Unknown error" handler.

Regardless, with the context you've provided I don't know the cause of the AndroidRuntimeException, as AndroidRuntimeException is Android's way of saying "something went wrong -- sorry!", and for some reason your exception output is truncated, so we don't even see what the exception message is. :-/

At minimum, what you can do is get the actual exception message:

	try {
		StartActivity (new Intent ("blah"));
	}
	catch (Java.Lang.Throwable t) {
		IntPtr toString = JNIEnv.GetMethodID (Java.Lang.Class.Object,
			"toString",
			"()Ljava/lang/String;");
		Android.Util.Log.Info ("what?", "Throwable caught; message={0}",
			JNIEnv.GetString (JNIEnv.CallObjectMethod (t.Handle, toString), true);
	}

Ideally, you could also attach your project so that we can see what's going wrong. Is your `context` instance an Activity? Or is it a Service or some other Context subclass? Is there something wrong with the use of your [Activity] attribute? etc.

Thanks,
 - Jon
Comment 2 Philippe Girolami 2011-09-29 04:11:12 UTC
Thanks for your help Jon.

I'm not starting my activity via an action but by using an explicit Class. That class has the following right above its definition:
[Activity(Label = "blablabla", MainLauncher = false, Icon = "@drawable/icon")]

Using the JNI call to get the underlying exception, the resulting log message is:
I/what?   ( 6139): Throwable caught; message=android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Which does explain it : I'm using the Application object as context for the intent so it's complaining about that. It was the first time I tried it and I got burned...

And to answer all your questions, running
      StartActivity (new Intent ("blah"));
I do get the Java exception:
I/MonoDroid( 5986): UNHANDLED EXCEPTION: Android.Content.ActivityNotFoundException: Exception of type 'Android.Content.ActivityNotFoundException' was thrown.
I/MonoDroid( 5986): at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) <0x00088>
I/MonoDroid( 5986): at Android.Content.ContextWrapper.StartActivity (Android.Content.Intent) <0x0020f>
I/MonoDroid( 5986): at MIAndroid.Login.<OnCreate>b__0 (object,System.EventArgs) <0x00047>
I/MonoDroid( 5986): at Android.Views.View/IOnClickListenerImplementor.OnClick (Android.Views.View) <0x0005b>
I/MonoDroid( 5986): at Android.Views.View/IOnClickListenerAdapter.n_OnClick_Landroid_view_View_ (intptr,intptr,intptr) <0x0005b>
I/MonoDroid( 5986): at (wrapper dynamic-method) object.dca0c23d-bc2f-4de7-bf6b-bdcccc8d595a (intptr,intptr,intptr) <0x00033>
I/MonoDroid( 5986): 
I/MonoDroid( 5986):   --- End of managed exception stack trace ---
I/MonoDroid( 5986): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=blah }
I/MonoDroid( 5986): 	at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
I/MonoDroid( 5986): 	at android.app.Instrumentation.execStartActivity(Instrumentation.java:1382)
I/MonoDroid( 5986): 	at android.app.Activity.startActivityForResult(Activity.java:3131)
I/MonoDroid( 5986): 	at android.app.Activity.startActivity(Activity.java:3237)
I/MonoDroid( 5986): 	at mono.android.view.view.IOnClickListenerAdapter.n_onClick(Native Method)
I/MonoDroid( 5986): 	at mono.android.view.view.IOnClickListenerAdapter.onClick(IOnClickListenerAdapter.java:9)
I/MonoDroid( 5986): 	at android.view.View.performClick(View.java:3110)
I/MonoDroid( 5986): 	at android.view.View$PerformClick.run(View.java:11934)
I/MonoDroid( 5986): 	at android.os.Handler.handleCallback(Handler.java:587)
I/MonoDroid( 5986): 	at android.os.Handler.dispatchMessage(Handler.java:92)
I/MonoDroid( 5986): 	at android.os.Looper.loop(Looper.java:132)
I/MonoDroid( 5986): 	at android.app.ActivityThread.main(ActivityThread.java:4123)
I/MonoDroid( 5986): 	at java.lang.reflect.Method.invokeNative(Native Method)
I/MonoDroid( 5986): 	at java.lang.reflect.Method.invoke(Method.java:491)
I/MonoDroid( 5986): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
I/MonoDroid( 5986): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
I/MonoDroid( 5986): 	at dalvik.system.NativeStart.main(Native Method)
E/mono    ( 5986): 
E/mono    ( 5986): Unhandled Exception: Android.Content.ActivityNotFoundException: Exception of type 'Android.Content.ActivityNotFoundException' was thrown.
E/mono    ( 5986):   at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00000] in <filename unknown>:0 
E/mono    ( 5986):   at Android.Content.ContextWrapper.StartActivity (Android.Content.Intent intent) [0x00000] in <filename unknown>:0 
E/mono    ( 5986):   at MIAndroid.Login.<OnCreate>b__0 (System.Object , System.EventArgs ) [0x00000] in <filename unknown>:0 
E/mono    ( 5986):   at Android.Views.View+IOnClickListenerImplementor.OnClick (Android.Views.View v) [0x00000] in <filename unknown>:0 
E/mono    ( 5986):   at Android.Views.View+IOnClickListenerAdapter.n_OnClick_Landroid_view_View_ (IntPtr jnienv, IntPtr native__this, IntPtr native_v) [0x00000] in <filename unknown>:0 
E/mono    ( 5986):   at (wrapper dynamic-method) object:dca0c23d-bc2f-4de7-bf6b-bdcccc8d595a (intptr,intptr,intptr)
E/mono    ( 5986):   --- End of managed exception stack trace ---
E/mono    ( 5986): android.content.ActivityNotFoundException: No Activity found to
D/Zygote  (   82): Process 5986 exited cleanly (1)
Comment 3 Jonathan Pryor 2011-09-29 11:34:13 UTC
I find it rather odd that your original code didn't show the Java-side exception info, but the new code does.

How are you using the Application context? If I take the default app template and modify to use ApplicationContext.StartActivity() instead of StartActivity():


	button.Click += delegate {
		button.Text = string.Format ("{0} clicks!", count++);
		try
		{
			this.ApplicationContext.StartActivity (new Intent (this, typeof (Activity1)));
		}
		catch (Exception ex)
		{
			Android.Util.Log.Info ("*jonp*", "Exception: {0}", ex);
			button.Text = string.Format("Exception: {0}", ex.Message);
		}
	};

then launch the app and tap the button, I continue to get the Java-side exception:


I/*jonp*  (16903): Exception: Android.Util.AndroidRuntimeException: Exception of type 'Android.Util.AndroidRuntimeException' was thrown.
I/*jonp*  (16903):   at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00000] in <filename unknown>:0 
I/*jonp*  (16903):   at Android.Content.ContextWrapper.StartActivity (Android.Content.Intent intent) [0x00000] in <filename unknown>:0 
I/*jonp*  (16903):   at ScratchARE.Activity1+<OnCreate>c__AnonStorey0.<>m__0 (System.Object , System.EventArgs ) [0x00000] in <filename unknown>:0 
I/*jonp*  (16903):   --- End of managed exception stack trace ---
I/*jonp*  (16903): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I/*jonp*  (16903): 	at android.app.ContextImpl.startActivity(ContextImpl.java:626)
I/*jonp*  (16903): 	at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
I/*jonp*  (16903): 	at mono.android.view.View.OnClickListenerImplementor.n_onClick(Native Method)
I/*jonp*  (16903): 	at mono.android.view.View.OnClickListenerImplementor.onClick(OnClickListenerImplementor.java:20)
I/*jonp*  (16903): 	at android.view.View.performClick(View.java:2485)
I/*jonp*  (16903): 	at android.view.View$PerformClick.run(View.java:9080)
I/*jonp*  (16903): 	at android.os.Handler.handleCallback(Handler.java:587)
I/*jonp*  (16903): 	at android.os.Handler.dispatchMessage(Handler.java:92)
I/*jonp*  (16903): 	at android.os.Looper.loop(Looper.java:130)
I/*jonp*  (16903): 	at android.app.ActivityThread.main(ActivityThread.java:3683)
I/*jonp*  (16903): 	at java.lang.reflect.Method.invokeNative(Native Method)
I/*jonp*  (16903): 	at java.lang.reflect.Method.invoke(Method.java:507)
I/*jonp*  (16903): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
I/*jonp*  (16903): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
I/*jonp*  (16903): 	at dalvik.system.NativeStart.main(Native Method)

I'm not sure why your original invocation didn't show the Java-side exception, and that's worrying. :-/
Comment 4 Philippe Girolami 2011-09-29 11:41:45 UTC
I was using the application object itself: http://developer.android.com/reference/android/app/Application.html

And, because it might matter, I was doing this inside a .NET Thread launched through a button clikc delegate.