gpt4 book ai didi

android - 从ndk中的不同线程调用Java方法

转载 作者:行者123 更新时间:2023-12-03 12:59:30 25 4
gpt4 key购买 nike

我正在尝试使用android的NDK从c++中的分离线程中调用java静态方法。

到目前为止,我已经:

    JNIEnv *env =AttachJava();

jclass cls2 = env->FindClass("com/actvt/showdown/pluggin/Utils"); // try to find the class
//jmethodID mid2 = env->GetStaticMethodID(cls2, "AddSound", "(Landroid/app/Activity;Ljava/lang/String;I)V"); // find method
jmethodID mid = env->GetStaticMethodID(cls2, "addFrame", "(Landroid/app/Activity;JIIII)V"); // find method
jclass dataClass = env->FindClass("android/app/Activity");
jobject javaClassRef = env->AllocObject(dataClass);
env->CallStaticVoidMethod(cls2, mid,javaClassRef,(long)frame,0,footageIndex,frameNumber,width,height);

我将这个线程附加到jvm上,其名称为AttachJava wich:
 JNIEnv* Wrapper::AttachJava()
{
JNIEnv* myNewEnv;
JavaVMAttachArgs args;
args.version = JNI_VERSION_1_6; // choose your JNI version
args.name = nullptr; // you might want to give the java thread a name
args.group = nullptr; // you might want to assign the java thread to a ThreadGroup
Wrapper::jvm->AttachCurrentThread(&myNewEnv, &args);
return myNewEnv;
}

而且我在运行时遇到以下错误:
06-14 10:04:31.548 32423-484/com.learnopengles.airhockey A/art: art/runtime/java_vm_ext.cc:470] JNI DETECTED ERROR IN APPLICATION: JNI GetStaticMethodID called with pending exception java.lang.ClassNotFoundException: Didn't find class "com.actvt.showdown.pluggin.Utils" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib, /vendor/lib, /system/lib, /vendor/lib]]
06-14 10:04:31.548 32423-484/com.learnopengles.airhockey A/art: art/runtime/java_vm_ext.cc:470] at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
06-14 10:04:31.548 32423-484/com.learnopengles.airhockey A/art: art/runtime/java_vm_ext.cc:470] at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
06-14 10:04:31.548 32423-484/com.learnopengles.airhockey A/art: art/runtime/java_vm_ext.cc:470] at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)

如果您还有其他需要,我随时为您服务

问候,

最佳答案

解决我的问题的方法是,在启动线程之前,请在ndk调用开始时保留对我在不同线程中所需的所有类的引用,然后在这些线程中使用它们。我也可以将它们作为参数提供给线程。

找到了要去pskink并根据此文档:https://developer.android.com/training/articles/perf-jni.html#faq_FindClass

关于android - 从ndk中的不同线程调用Java方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44539053/

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