gpt4 book ai didi

java - 为什么调用 CallStaticVoidMethod 在 JNI 中抛出使用无效的 jobject

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:47 25 4
gpt4 key购买 nike

我是JNI初学者,当我尝试调用MainActivity中的native方法时,在native方法中。 CallStaticVoidMethod 中的静态方法通过本地方法调用 MainActivity。然后程序崩溃:

这是本地方法代码:

void Java_com_lijing_ndkjnidemeo_activity_MainActivity_staticMethod(JNIEnv *env, jobject obj, jobject aty) {
LOGI("native_staticMethod");
jclass cls;
jmethodID mid;
//1.Get the class method id
cls = (*env)->GetObjectClass(env, aty);
mid = (*env)->GetStaticMethodID(env, cls, "changeStr", "()V");
if (mid == NULL) {
LOGE("GetStaticMethodID error");
return; /* method not found */
}
LOGE("GetStaticMethodID sucess");
//2.Call the CallStatic<ReturnValueType>Method function to call the corresponding function.
(*env)->CallStaticVoidMethod(env, cls, mid);
}

MainActivity 中的本地方法调用:

private void jniTextMethod8() {
staticMethod(this);
tvStaticMethod.setText(sStr);
}

public static native String staticMethod(MainActivity aty);

public static void changeStr() {
sStr = "Change String";
}

日志:

11-07 09:57:35.477 4310-4310/com.lijing.ndkjnidemeo I/JNI_UTIL: native_staticMethod
11-07 09:57:35.477 4310-4310/com.lijing.ndkjnidemeo E/JNI_UTIL: GetStaticMethodID sucess
11-07 09:57:35.493 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0xffde5e68
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] from java.lang.String com.lijing.ndkjnidemeo.activity.MainActivity.staticMethod(com.lijing.ndkjnidemeo.activity.MainActivity)
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] "main" prio=5 tid=1 Runnable
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] | group="main" sCount=0 dsCount=0 obj=0x73712258 self=0xf3df4a00
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] | sysTid=4310 nice=0 cgrp=default sched=0/0 handle=0xf777cc00
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] | state=R schedstat=( 304952424 35818763 258 ) utm=12 stm=17 core=1 HZ=100
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] | stack=0xff5e8000-0xff5ea000 stackSize=8MB
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] | held mutexes= "mutator lock"(shared held)
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] native: #00 pc 0058b0e2 /system/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, char const*, art::ArtMethod*, void*)+226)
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] native: #01 pc 00550d2e /system/lib/libart.so (art::Thread::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const+286)
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] native: #02 pc 003a489f /system/lib/libart.so (art::JavaVMExt::JniAbort(char const*, char const*)+1247)
11-07 09:57:35.494 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] native: #03 pc 003a5fc2 /system/lib/libart.so (art::JavaVMExt::JniAbortF(char const*, char const*, ...)+116)
11-07 09:57:35.495 4310-4310/com.lijing.ndkjnidemeo A/art: art/runtime/java_vm_ext.cc:410] native: #04 pc 00548fc7 /system/lib/libart.so (art::Thread::DecodeJObject(_jobject*) const+1975)

最佳答案

您没有从您的 native 函数返回任何内容。

native 函数在java中定义为:

public static native String staticMethod(MainActivity aty);

但在 C 中为

void 
Java_com_lijing_ndkjnidemeo_activity_MainActivity_staticMethod(
JNIEnv *env,
jobject obj,
jobject aty)

您要么需要将 Java 更改为

    public static native void staticMethod(MainActivity aty);

或者你的 C 到类似的东西:

jobject 
Java_com_lijing_ndkjnidemeo_activity_MainActivity_staticMethod(
JNIEnv *env,
jobject obj,
jobject aty) {
..
return something;
}

关于java - 为什么调用 CallStaticVoidMethod 在 JNI 中抛出使用无效的 jobject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40456870/

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