查看: 2656|回复: 4
打印 上一主题 下一主题

[其他] 调用android方法出现!getMethodID.....FAILED的原因

[复制链接]

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2014-7-29 21:42:17 |只看该作者 |倒序浏览

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的方法是确保这些函数返回一些东西,或者它们不能创建另一个方法或其他一些类似的以确保你要返回的值是有效的。

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

无效楼层,该帖已经被删除
hyui    

1

主题

2

听众

6671

积分

高级设计师

Rank: 6Rank: 6

纳金币
2715
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2014-7-29 22:03:21 |只看该作者
Thanks for this share !!!!
回复

使用道具 举报

无效楼层,该帖已经被删除

115

主题

3

听众

5676

积分

高级设计师

Rank: 6Rank: 6

纳金币
7268
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2014-7-29 22:41:58 |只看该作者
Thanks for sharing this one !
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-20 11:59 , Processed in 0.087827 second(s), 33 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部