JNI错误:java.lang.NoSuchMethodError:无静态方法

时间:2019-06-11 08:22:28

标签: c++ android-ndk java-native-interface

错误消息: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->无效更改 => 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);
}

CHECK.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

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

0 个答案:

没有答案