[英]Android NDK method not found…calling Java from C++
I want to call a Java method from C++. 我想从C ++调用Java方法。 I've followed other examples I've found, but I keep getting the error: 我遵循了我发现的其他示例,但是不断出现错误:
GetMethodID: method not found: Lcom/test/ndk/GL2JNIActivity;.GetFiles:(L/java/lang/String;Ljava/lang/String;Z;)V
What am I doing wrong here? 我在这里做错了什么? I suspect my method signature string is wrong for env->GetMethodID(), but it looks like it's correct from other examples I've seen. 我怀疑我的方法签名字符串对于env-> GetMethodID()是错误的,但是从我看过的其他示例来看,这似乎是正确的。
C++: C ++:
namespace Resource
{
FileInfo *GetFiles(const char *path, const char *mask, bool directory)
{
JNIEnv *env = Android_JNI_GetEnv();
jstring jstr1 = env->NewStringUTF(path);
jstring jstr2 = env->NewStringUTF(mask);
fileInfos = NULL;
jclass clazz = env->FindClass("com/test/ndk/GL2JNIActivity");
jmethodID mid = env->GetMethodID(clazz, "GetFiles", "(Ljava/lang/String;Ljava/lang/String;Z;)V");
jobject obj = env->CallStaticObjectMethod(clazz, mid);
return fileInfos;
}
}
Java: Java的:
package com.test.ndk;
import java.io.File;
public class GL2JNIActivity extends Activity implements SensorEventListener {
public static void GetFiles(String sentPath, String mask, boolean directory)
{
String path = Environment.getExternalStorageDirectory().toString()+sentPath;
Log.d("Files", "Path: " + path);
File f = new File(path);
File file[] = f.listFiles();
Log.d("Files", "Size: "+ file.length);
for (int i=0; i < file.length; i++)
{
Log.d("Files", "FileName:" + file[i].getName());
GL2JNILib.AddFileToList(file[i].getName(), file[i].isDirectory());
}
}
}
You should replace 你应该更换
jmethodID mid = env->GetMethodID(clazz, "GetFiles", "(Ljava/lang/String;Ljava/lang/String;Z;)V");
by 通过
jmethodID mid = env->GetMethodID(clazz, "GetFiles", "(Ljava/lang/String;Ljava/lang/String;Z)V");
because last parameter is a boolean that is not a fully qualified class but a basic type signature so no ;
因为last参数是一个布尔值,不是完全限定的类,而是一个基本的类型签名;
is needed. 是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.