- Home /
AndroidJavaObject.Call fails with 'method not found'
I created a Java Activity that I want to start from a C# script running in Unity.
To do so, I made this routine:
AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
Debug.Log("intentObject created");
intentObject.Call("setClassName", "com.twnkls.SendImageWithIntent", "SendImageWithIntentActivity");
However, the 'Call' method fails with a "GetMethodID: method not found: Landroid/content/;.setClassName error. This causes the application to crash (no exception handling in place yet).
This is relevant section of the log from LogCat:
06-30 18:34:57.053: INFO/Unity(16440): intentObject created
06-30 18:34:57.053: INFO/Unity(16440): UnityEngine.Debug:Internal_Log(Int32, String, Object)
06-30 18:34:57.053: INFO/Unity(16440): UnityEngine.Debug:Log(Object)
06-30 18:34:57.053: INFO/Unity(16440): SaveScreenshotScript:OnGUI() (at XXXmy-local-file-locationXXX\SaveScreenshotInUnityAndroid\Assets\SaveScreenshotScript.cs:37)
06-30 18:34:57.053: INFO/Unity(16440):
06-30 18:34:57.053: INFO/Unity(16440): (Filename: C Line: 0)
06-30 18:34:57.073: DEBUG/dalvikvm(16440): GetMethodID: method not found: Landroid/content/Intent;.setClassName:(Ljava/lang/String;Ljava/lang/String;)V
06-30 18:34:57.083: INFO/Unity(16440): JNI: Unable to find method id for 'setClassName'
06-30 18:34:57.083: INFO/Unity(16440): UnityEngine.Debug:Internal_Log(Int32, String, Object)
06-30 18:34:57.083: INFO/Unity(16440): UnityEngine.Debug:Log(Object)
06-30 18:34:57.083: INFO/Unity(16440): UnityEngine._AndroidJNIHelper:GetMethodID(IntPtr, String, Object[], Boolean)
06-30 18:34:57.083: INFO/Unity(16440): UnityEngine.AndroidJNIHelper:GetMethodID(IntPtr, String, Object[], Boolean)
06-30 18:34:57.083: INFO/Unity(16440): UnityEngine.AndroidJavaObject:GetCachedMethodID(String, Object[], Boolean)
06-30 18:34:57.083: INFO/Unity(16440): UnityEngine.AndroidJavaObject:_Call(String, Object[])
06-30 18:34:57.083: INFO/Unity(16440): UnityEngine.AndroidJavaObject:Call(String, Object[])
06-30 18:34:57.083: INFO/Unity(16440): SaveScreenshotScript:OnGUI() (at XXXmy-local-file-locationXXX\SaveScreenshotInUnityAndroid\Assets\SaveScreenshotScript.cs:38)
It's strange because according to the Android docs, the setClassName method in android.content.Intent has been around since API level 1.
To verify that calling methods on Java objects works at all in my environment: when I use the sample code on Unity's AndroidJavaObject documentation page, the script runs without problem, so that's not it.
Perhaps I'm making an error in the parameters to the method? Although one would think that passing two String objects would match the signature..
I'm pretty new to Android development, so I hope someone can help me out here..
Answer by Tseng · Sep 08, 2011 at 02:08 PM
Even though this is pretty old and you probably already found it out by yourself...
But the setClassName
method has a return value and you're calling Call method for methods without a return value (void).
The correct way of calling the method is
intentObject.Call<AndroidJavaObject>("setClassName", "com.twnkls.SendImageWithIntent", "SendImageWithIntentActivity");
because, setClassName
returns an Intent, as you can see in the Android Documentation for the Intent class.