gpt4 book ai didi

android - 方法签名与实际调用不匹配

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:06:54 25 4
gpt4 key购买 nike

在 android 上使用 java 原生接口(interface)时,我犯了两个愚蠢的错误,浪费了我很多时间。

有这个方法id:

jmethodID myMethod_methodID = env->GetMethodID(hello_Cls, "myMethod", "(ILjava/lang/String;Ljava/lang/String;I)Z");

我的第一个错误是使用

env->CallVoidMethod

我的第二个错误是这样调用它

jboolean rv = jenv->CallBooleanMethod(hello_obj, myMethod_methodID, myfirst_jstring, mysecond_jstring, 1);

这显然缺少 myMethod_methodIDmyfirst_jstring 之间的 jint 参数。

我花了很长时间来追踪这些错误,因为 logcat 中没有相关输出,唯一的行为是什么都不做(它甚至没有崩溃)。

因此,问题是:如何针对此类错误获得更有意义的错误?

最佳答案

此页面可能有用:http://www.netmite.com/android/mydroid/dalvik/docs/jni-tips.html ,特别是:

JNI does very little error checking. Calling SetFieldInt on an Object field will succeed. The goal is to minimize the overhead on the assumption that, if you've written it in native code, you probably did it for performance reasons.

Some VMs support extended checking with the "-Xcheck:jni" flag. If the flag is set, the VM puts a different table of functions into the JavaVM and JNIEnv pointers. These functions do an extended series of checks before calling the standard implementation.

Some things that may be verified:

  • Check for null pointers where not allowed.
  • Verify argument type correctness (jclass is a class object, jfieldID points to field data, jstring is a java.lang.String).
  • Field type correctness, e.g. don't store a HashMap in a String field.
  • Check to see if an exception is pending on calls where pending exceptions are not legal.
  • Check for calls to inappropriate functions between Critical get/release calls.
  • Check that JNIEnv structs aren't being shared between threads.
  • Make sure local references aren't used outside their allowed lifespan.
  • UTF-8 strings contain valid "modified UTF-8" data.

Accessibility of methods and fields (i.e. public vs. private) is not checked.

点击链接:http://www.netmite.com/android/mydroid/dalvik/docs/embedded-vm-control.html

您可以按如下方式设置 adb:

adb shell setprop dalvik.vm.checkjni true
adb shell setprop dalvik.vm.execution-mode int:debug

关于android - 方法签名与实际调用不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5743570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com