- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试从 Android 应用程序中的 native C 代码调用 java 方法。使用 JNI 这听起来很简单,但我的代码在最终调用方法本身时总是崩溃。这是我的代码: native C 代码:
JNIEXPORT void JNICALL
Java_com_path_to_my_package_renderStuff(JNIEnv* env, jobject jobj){
//...
jclass clazz = env->FindClass("com/path/to/the/class");
jmethodID showCar = env->GetMethodID(clazz,"showCar","()V" );
env->CallVoidMethod(jobj,showCar); //If I comment this out, it won't crash
//...
}
Java 代码:
public void showCar(){
doSomething()
}
doSomething() 甚至没有达到,我可以在那里设置一个断点,它永远不会被击中。如上所述,一旦我注释掉 CallVoidMethod 调用,它就不会崩溃,但显然也不会调用 showCar() 。有什么提示吗?
最佳答案
为您提供的 4 个想法:
...
jclass clazz = env->FindClass("com/path/to/the/class");
您能否确认名称不是“com/path/to/the/MyClass”,其中类名是大写的第一个字符,显然名称“class”是保留字。在您的示例中,JNI C 符号名称“Java_com_path_to_my_package_renderStuff”的使用与“com/path/to/the/class”上的 FindClass() 查找之间存在细微差异。但是由于您的 stackoverflow 与 UnsatisfiedLinkageError 无关,我只能猜测您提供的示例与自身不一致。
使用我的示例,我希望 JNI C 符号名称为“Java_com_path_to_the_MyClass_renderStuff”,FindClass() 查找“com/path/to/the/MyClass”。使用类的第一个大写字母和方法名称的第一个小写字母对于链接目的可能很重要。
...
您确定传递的“jobj”与您正在查找的“com/path/to/the/class”是同一类型吗?也许在您的 Java 代码中,您可以使用以下代码包装您的原生代码:
public void renderStuff() {
if((this instanceof com.path.to.the.MyClass) == false)
throw new RuntimeException("Unexpected class expected: com.path.to.the.MyClass");
renderStuff_internal();
}
private native void renderStuff_internal();
这将确保在 Java 代码中很重要,而不会导致 JVM 崩溃。您还需要调整您的 C 符号名称以将“_1internal”附加到“Java_com_path_to_the_MyClass_renderStuff_1internal”的末尾(额外的“1”字符是有意的)
...
也许在你列出的每条语句之间尝试带和大括号异常检查:
if(env->ExceptionCheck()) {
env->ExceptionDescribe();
env->ExceptionClear();
}
这将在可能不允许的情况下尝试进行反射时发现诸如安全违规之类的事情。
...
jclass cls = env->GetObjectClass(jobj); // instead of FindClass
jmethodID mid = env->GetMethodID(cls, "showCar", "()V");
if(!mid) return; // whoops method does not exist
env->CallVoidMethod(jobj, mid);
删除 FindClass() 调用的另一个想法。这适用于 GetMethodID 处理的任何类,有点像动态输入/后期绑定(bind)。
关于android - 调用 CallVoidMethod 时 JNI 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7530821/
我正在从我的 jni 类中调用一个非静态函数,但我的游戏得到了 NoSuchMethodError 我已经给出了下面的函数 jni类 void InitializeJni() { cocos2
我有一个带有一些 jni native 方法的 java 应用程序。其中之一是 readFile,它是这样调用的: OutputStream outStream = null; File file =
我正在尝试从 android-ndk 内发送短信。当我尝试调用 SmsManager 中的 sendTextMessage 方法时,我的应用程序崩溃,错误代码为 6(中止)。此外,我收到一条错误消息“
我正在尝试从 Android 应用程序中的 native C++ 代码调用 java 方法。当我尝试调用我的 Java 方法时发生异常。我看过几个例子,但我无法弄清楚我做错了什么。请帮助我。 Java
我正在尝试从 Android 应用程序中的 native C 代码调用 java 方法。使用 JNI 这听起来很简单,但我的代码在最终调用方法本身时总是崩溃。这是我的代码: native C 代码:
我的目标是在 C 代码中建立一个无限循环,执行将 String 传递给 Java 函数的回调。然后,Java 函数使用String 来更新文本框的内容。问题是回调尝试生成以下错误: JNI DETEC
当为我的 UDP 程序调用我的 JNI 函数 CallVoidMethod 时,我的程序崩溃了。我无法从 logcat 破译问题。任何帮助将不胜感激。 我的代码 fragment : JNI 函数:
我想传递从 Java 类中的 native 方法计算的数组数组。为此,我想从 native 代码调用方法 setTarghet 来设置每个 vector 场。我认为这是个好主意。这是一个 Java 类
我是一名优秀的程序员,十分优秀!