简体   繁体   English

使用JNI从C ++调用Java函数:无法找到静态方法ID

[英]Calling java function from c++ using jni: Failed to find static method id

I'm creating cocos2d-x game (v. 3.4) on mac os using android sdk 4.2.2, but I tried other too. 我正在使用android sdk 4.2.2在Mac OS上创建cocos2d-x游戏(v。3.4),但我也尝试了其他方法。 I want to make NativeHelper class to call some native android stuff from c++. 我想让NativeHelper类从c ++调用一些本地android的东西。 I used this tutorial as the base: http://www.cocos2d-x.org/wiki/User_Tutorial-Integrate_AdMob It's working, but I want use a java function with parameters. 我以本教程为基础: http : //www.cocos2d-x.org/wiki/User_Tutorial-Integrate_AdMob可以,但是我想使用带有参数的Java函数。 And there comes errors: 并出现错误:

02-13 09:33:20.690: W/dalvikvm(28873): Bogus method descriptor: (I;)V 02-13 09:33:20.690: E/JniHelper(28873): Failed to find static method id of showBanner 02-13 09:33:20.690:W / dalvikvm(28873):伪方法描述符:(I;)V 02-13 09:33:20.690:E / JniHelper(28873):无法找到showBanner的静态方法ID

Here's java implementation: 这是Java实现:

public static void showBanner(int position) {
        final int _position = position;
        _appActivity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if(_appActivity != null){
                    if (!_appActivity.adView.isEnabled()){
                        _appActivity.adView.setEnabled(true);
                    }
                    if (_appActivity.adView.getVisibility() == View.INVISIBLE){
                        _appActivity.adView.setVisibility(View.VISIBLE);
                        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                        if(_position == 0){
                            adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                        }
                        else{
                            adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);                           
                        }
                        _appActivity.adView.setLayoutParams(adParams);
                    }
                }
            }
        });
    }

c++ implementation: C ++实现:

void NativeHelper::showBanner(int position){
    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t, AppActivityClassName, "showBanner", "(I;)V")){

        t.env->CallStaticVoidMethod(t.classID, t.methodID, position);

        t.env->DeleteLocalRef(t.classID);

        isBannerShowing = true;
    }
}

If I just remove position parameter from both function (and change (I;)V to ()V ) it's working like a charm. 如果我只是从两个函数中都删除位置参数(并将(I;)V更改为()V),则它的工作就像一个魅力。 I tried other parameter types like bool and it also doesn't work. 我尝试了其他参数类型(例如bool),但它也不起作用。

I thought I maybe did something wrong so I found this tutorial: http://stnguyen.com/cocos2d-x/call-java-functions-from-cpp.html 我以为我可能做错了什么,所以我找到了本教程: http : //stnguyen.com/cocos2d-x/call-java-functions-from-cpp.html

And calling for example sayHello also doesn't work too: 并说sayHello也不起作用:

02-13 09:33:16.955: W/dalvikvm(28873): Bogus method descriptor: (Ljava/lang/String;I;)V 02-13 09:33:16.955: E/JniHelper(28873): Failed to find static method id of sayHello 02-13 09:33:16.955:W / dalvikvm(28873):伪造的方法描述符:(Ljava / lang / String; I;)V 02-13 09:33:16.955:E / JniHelper(28873):找不到sayHello的静态方法ID

I'm using ndk r9, but tried r10 too. 我正在使用ndk r9,但也尝试过r10。 I'm basically out of ideas... 我基本上没有主意...

You mistyped. 你打错了。 It should be "(I)V", not "(I;)V" 它应该是“(I)V”,而不是“(I;)V”

You can just open the compiled .class file, and search the method name. 您可以打开已编译的.class文件,然后搜索方法名称。 The compiled file will be a hex file, but method name and parameter is text. 编译后的文件将是一个十六进制文件,但是方法名称和参数是文本。 see Sample 样品

public void InitUserData(int sdk_app_id, String account_type, String 
app_id_3rd, String identifier, String user_sig)

=======>

InitUserDataL(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V

Use javap is also ok, but open the .class file is more convenient. 使用javap也可以,但是打开.class文件更为方便。

For me a double myfunction(java.lang.String astringparam) {} needed (Ljava/lang/String;)D . 对我来说,需要double myfunction(java.lang.String astringparam) {} (Ljava/lang/String;)D (Ljava/lang/String)D without the semicolon did not work. 不带分号的(Ljava / lang / String)D不起作用。

BTW I used "javap -s -classpath bin/classes/org/cocos2dx/javascript/ AppActivity" on the command line to get the signature. 顺便说一句,我在命令行上使用了“ javap -s -classpath bin / classes / org / cocos2dx / javascript / AppActivity”来获取签名。 (Thanks to @EJP) (感谢@EJP)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM