c++ - JNI 错误:java.lang.NoSuchMethodError:没有静态方法
问题描述
错误信息 :Pending exception java.lang.NoSuchMethodError thrown by 'void com.exampleTest.NDKT.I(android.app.Activity):-2'
java.lang.NoSuchMethodError: no static method "Lcom/exampleTest/NDKT;.PN()Ljava/lang/String;"
我应该尝试 ndk c++ 调用 java 方法,因为我希望你知道 java 方法认为 android 包名称
我在下面试过
- java 调用方法 String -> void change => c++ : jmethodID jMethod = pE->GetStaticMethodID(jClass, "GO", "()V");
- java代码“this”->“MainActivity.this”
- Java 代码
CallNDK class{
public static void initialize(final Activity a,final SIMECB cb) {
System.loadLibrary("simpleinit");
_CB = cb;
_INSTANCE = new CallNDK();
new Thread(new Runnable() {
public void run() {
_INSTANCE.I(a);
}
}).start();
}
}
==> initialize () -> Activity = MainActivity.this
- NDK
调用NDK.cpp
static string mPN; //Packege Name
#ifdef __ANDROID__
void CallNDK::I(JNIEnv* pE, jobject pA){
// Package Name Method call
CHECK::PN(pE, mPN);
}
检查.h
#ifndef CHECK_H_
#define CHECK_H_
#ifdef __ANDROID__
#include <jni.h>
#endif
using namespace std;
class CHECK {
public:
#ifdef __ANDROID__
static void PN(
JNIEnv* pE,
string& pO);
#endif
};
#endif
检查.cpp
#include "CHECK.h"
#ifdef __ANDROID__
#include <jni.h>
#endif
using namespace std;
#ifdef __ANDROID__
void CHECK::PN(JNIEnv* pE, string& pO){
jclass classes = pE->FindClass("com/exampleTest/NDKT");
jmethodID jMethod = pE->GetMethodID(classes, "SimpleMethod", ()Ljava/lang/String;");
if(!jMethod) {
CALLUtil::LOG("class loader method access");
return;
}
jstring jsResult = (jstring)pE->CallStaticObjectMethod(jClass, jMethod);
const char* cResult = pE->GetStringUTFChars(jsResult, NULL);
std::string sResult(cResult);
pO = sResult;
}
- 调用java类代码的NDK
enter code here
NDKT Class {
private static int _MAX_TRY_INVOKE_COUNT = 5;
public static String SimpleMethod(Activity a){
for(int i = 0; i < _MAX_TRY_INVOKE_COUNT; i++) {
try {
return a.getApplicationContext().getPackageName();
} catch (Exception e) {
try { Thread.sleep(50); } catch (Exception e2) { }
}
}
return ""
}
}
- 错误 JNI 在应用程序中检测到错误:
JNI FindClass called with pending exception 'java.lang.NoSuchMethodError' thrown in void com.exampleTest.CallNDK.I(android.app.Activity):-2 in call to FindClass from void com.exampleText.CallNDK.I(android.app.Activity)
Pending exception java.lang.NoSuchMethodError thrown by 'void com.exampleTest.NDKT.I(android.app.Activity):-2'
java.lang.NoSuchMethodError: no static method "Lcom/exampleTest/NDKT;.PN()Ljava/lang/String;"
解决方案
推荐阅读
- grpc - Grpc 为特定端点编写拦截器
- python - Pandas DataFrame 创建在循环中引发 ValueError
- laravel - 避免四舍五入 - Laravel
- ruby - 如何在 Ruby 中生成 WM_SEC.AUTH_SIGNATURE?
- kubernetes - 具有资源名称和列表对象的 Kubernetes RBAC 角色
- c# - 如何注入泛型类 Sample 的所有注册实例
在点网核心? - javascript - JavaScript 仅覆盖属于对象的默认选项列表中的指定选项
- laravel - Laravel:如何将中间件添加到命名路由中?
- java - 为什么在 Java 中将一个变量重新分配给一个新的 String 需要这么多时间?
- javascript - For 循环似乎比预期的要运行两倍