纳金网

标题: nity调用android方法出现!getMethodID.....FAILED的原因 [打印本页]

作者: 狂风大尉    时间: 2014-6-30 18:40
标题: nity调用android方法出现!getMethodID.....FAILED的原因

It is likely that the Java object you are trying to access has gone out of scope, or has a return value other than void.
So, when you make the call to CallStatic, you should probably quality the return value as an AndroidJavaObject.
You've probably seen this type of code in plugin examples. In the 2nd using statement, it calls a static member function of UnityPlayer to get the current activity object. If I called unityClass.GetStatic("currentActivity") without the qualifier, I'd get an error saying "can't find method id".
Also, if for some reason the return value from the Java code were null, a different kind of error would happen, which is also pretty common, the "JNI: Init'd AndroidJavaObject with null ptr" error.
The best to to avoid that error is to go into the Java code and make sure that these functions return something, or if they can't create another method like "hasInstance" or some such that can make sure the value you are about to return is a valid one.

1,若果在虚拟机测试时,出现!GetMesthodID......FAILED,其原因可能是你选择访问的Java Object 已经超出范围,或者是该方法返回无效的值。
其出现的主要原因有几点:
1.调用的包名不对。
2.调用的方法名与jar里面定义的方法名不对。
3.Unity端传递的方法参数与jar特点的方法的参数不符。
4.Android端调用的方法出错(Android端方法返回无效的值)。
5.Android端方法返回类型出错。
所以每当你访问一个java object时,应该以AndroidJavaObject 作为静态返回值:unityClass.GetStatic<AndroidJavaObject>("currentActivity"))
如果没有用静态方法对限定符unityClass.GetStatic<AndroidJavaObject>
("currentActivity"))进行绑定,那么对 unityClass的第二次调用就很可能会出现错误说"can't find method id".同样,如果由于某种原因从Java代码返回值是null,一种不同的错误会发生,这也是很常见的JNI:Init AndroidJavaObject null ptr错误。

最好避免错误进入Java的方法是确保这些函数返回一些东西,或者它们不能创建另一个方法或其他一些类似的以确保你要返回的值是有效的。


作者: HIDEOKOJIMA    时间: 2014-6-30 18:53
Thanks for sharing !
作者: hyui    时间: 2014-6-30 21:47
Thanks for sharing this !
作者: wucnj    时间: 2014-7-1 09:48
感谢分享!!!




欢迎光临 纳金网 (http://course.narkii.com/club/) Powered by Discuz! X2.5