首页 > 解决方案 > 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 代码

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 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;"

标签: c++android-ndkjava-native-interface

解决方案


推荐阅读