0

当我尝试使用 GetMethodID 查找方法时,我的 JNI 代码一直收到此错误,我的 Java 方法位于接口中。

这是我的界面

 public interface printReader 
 {

 public printImg readerPrint(String selectedName) throws Exception;

 }

本机代码

WprintImgIMPL.h
class WprintImgIMPL: public IWprintReader  {

public:

    WprintImgIMPL(JNIEnv *env, jobject obj);
   ~WprintImgIMPL(void);

virtual WprintImg  readerPrint(char* readerName) ;
   .....
   .....
 private:
    JNIEnv *m_Env;
    jobject m_jObj;
 }

WprintImgIMPL.cpp
WprintImg  WprintImgIMPL::readerPrint(char* readerName) {
jclass cls = m_Env->GetObjectClass (m_jObj);

jmethodID mid = m_Env->GetMethodID (cls, "readerPrint", "(Ljava/lang/String;)Lcom/site/name/printImg;");
.......
.......
}

Java 代码

public class printReaderIMPL implements printReader {
static final String DEBUG_TAG = ""; 
android.net.wifi.WifiManager.MulticastLock lock;

Context _context;

public printReaderIMPL (Context context) {
    _context = context;
}

@Override
public printImg readerPrint(String selectedName) throws Exception { 

    Log.e(DEBUG_TAG, "readerPrint");
     }
}

构造函数/析构函数

   WprintImgIMPL(JNIEnv *env, jobject obj){
     m_Env = env;
     m_jobj = env->NewGlobalRef(obj);

  }
  ~WprintImgIMPL(void) {
     m_Env->DeleteGlobalRef(m_jobj);
  }

Error: GetMethodID: method not found: Lcom/site/name/NativeCode;.printImg:(Ljava/lang/String;)Lcom/site/name/printImg;

签名检查两次,失败后我再次使用 Javap 工具生成。

如果您可以输入/评论并帮助修复此错误,谢谢。

4

1 回答 1

1

It is invalid to save a JNIEnv* across JNI method calls. It's only valid for the duration of the JNI method you are currently in. Out of the blue, e.g. in arbitrary C++ code, you need to call AttachCurrentThread() to get a current valid JNIEnv*.

However you can cache the methodID. There no need to look it up every time. Look it up in your constructor.

于 2013-11-01T02:17:02.713 回答