David Karlsson
David Karlsson

Reputation: 9686

Callback from NDK not finding method in java

Im trying to implement a callback in JNI/NDK and android. But i can't get the method signatures or something right, Why can't the method be found?

My MainActivity instance calls the ndk function:

 void populate(JNIEnv* aEnv, jobject aObj, jstring str){

     ....


    jclass cls = aEnv->FindClass("com/example/jsonpoco/MainActivity");
    jmethodID mid = aEnv->GetMethodID(cls, "add", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;D;D)V");
    std::list<MyRefComplexType>::iterator it = ref->myRefs.begin();
    for(int i = 0; i < ref->myRefs.size()/100;i++){
      std::advance(it, 1);
      printf(it->getMyNo().c_str()); //<-Works
      jstring myNo = aEnv->NewStringUTF(it->getMyNo().c_str());
      jstring myName = aEnv->NewStringUTF(it->getMyName().c_str());
      jstring myUrl = aEnv->NewStringUTF(it->getMyUrl().c_str());
      aEnv->CallVoidMethod(aObj,mid, myNo, myName, myUrl, it->getMyLocation()->getLatitude(), it->getMyLocation()->getLongitude());

}

My Java class calling the Native code implements the following method for handling the callback:

    public void add(String myNo, String myName,String myUrl, double lat, double lon){
    storesWrapper.add(storeNo, storeName, storeUrl, lat, lon);
}

I get the following error:

04-09 16:31:42.431: W/dalvikvm(25852): JNI WARNING: JNI method called with exception pending
04-09 16:31:42.431: W/dalvikvm(25852):              in Lcom/example/jsonpoco/MainActivity;.json_parse:(Ljava/lang/String;)V (NewStringUTF)
04-09 16:31:42.431: W/dalvikvm(25852): Pending exception is:
04-09 16:31:42.431: I/dalvikvm(25852): java.lang.NoSuchMethodError: no method with name='add' signature='(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;D;D)V' in class Lcom/example/jsonpoco/MainActivity;

Upvotes: 2

Views: 3901

Answers (1)

Krypton
Krypton

Reputation: 3325

The method signature should be

(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;DD)V

There was a semicolon after D, which is wrong.

Upvotes: 4

Related Questions