[BUG] AndroidJavaObject getStringArrayExtra() crash
Hi, I have the following code
AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
//the previous intent that started this app
AndroidJavaObject intent = null;
try
{
intent = ca.Call<AndroidJavaObject>("getIntent"); //no exception ever happened(?)
}
catch (Exception e)
{
logText.text += e.ToString();
successGetIntent = false;
}
try
{
var strings = intent.Call<string[]>("getStringArrayExtra","KEY"); //this causes crash even in try catch if there no such extra send to this app
}
catch (Exception e)
{ //do smthng}
up.Dispose();
ca.Dispose();
intent.Dispose();
the problem is, everytime I call getStringArrayExtra
it will always crash and stop the unity, I cant even catch the exception. Is there anyway to solve this?
Thanks
Answer by kingandroid · Oct 06, 2015 at 11:15 PM
I solved this by myself and marked it as bug.
When I changed getStringArrayExtra
to getStringExtra
it is now not crashing (Obviously I change the sender sending string, instead of sending string[] previously)
That makes not much sense to "mark is as bug". Since it seems to work with "getStringExtra" the error was clearly on your side. "getStringArrayExtra" searches for a stored string array with the key "$$anonymous$$EY". Since it works with getStringExtra the key you're looking for seems to be a string and not a string array.
You're using the JNI interface that Java provides. If an exception occurs on the native Java side, Android will kill your app. Such an exception can't be caught from the managed side of $$anonymous$$ono.
It's also bad style to call Dispose "manually". Use "using" statements
string val = "";
using(AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using(AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity"))
using(AndroidJavaObject intent = ca.Call<AndroidJavaObject>("getIntent"))
{
val = intent.Call<string>("getStringExtra","$$anonymous$$EY");
} // at this point all 3 objects will be disposed in reverse order
You might want to look up getStringArrayExtra and what it actually does.
Like I said, when I'm using getStringArrayExtra i'm sending string[] and if the app can find the string[] it working perfectly. When it can't it just crashes - which is not the case when I'm sending string and get it with getStringExtra, if there is no string found it won't make the app crash.
Also in my newest version I'm using 'using'
but anyway thanks for the reply