2016-11-04 3 views
1

Когда я звоню testInterface(), SetName() в порядке, но setChild() следующим образом:Android JNI почему setChild() в ParentClass IsNot доступны

E/AndroidRuntime(18708): java.lang.NoSuchMethodError: no non-static method "Lcom/***/TestParent;.setChild(Ljava/lang/Object;)V" 

JNI

JNIEXPORT jobject JNICALL Java_***_testInterface(
     JNIEnv * env, jobject thiz) { 

    jclass parent_cls = env->FindClass("com/wkn/muscle/activity/TestParent"); 
    LOGD("testInterface 0"); 

    jmethodID parent_mid = env->GetMethodID(parent_cls, "<init>", "()V"); 
    LOGD("testInterface 1"); 

    jobject parent_obj = env->NewObject(parent_cls, parent_mid); 
    LOGD("testInterface 2"); 

    jstring str_parent = env->NewStringUTF("Parent_from_Native"); 
    LOGD("testInterface 3"); 

    jmethodID parent_set_name = env->GetMethodID(parent_cls, "setName", 
      "(Ljava/lang/String;)V"); 
    LOGD("testInterface 4"); 

    env->CallVoidMethod(parent_obj, parent_set_name, str_parent); 
    LOGD("testInterface 5"); 

    jclass child_cls = env->FindClass("com/wkn/muscle/activity/TestChild"); 
    LOGD("testInterface 6"); 

    jmethodID child_mid = env->GetMethodID(child_cls, "<init>", "()V"); 
    LOGD("testInterface 7"); 

    jobject child_obj = env->NewObject(child_cls, child_mid); 
    LOGD("testInterface 8"); 

    jstring str_child = env->NewStringUTF("Child_from_Native"); 
    LOGD("testInterface 9"); 

    jmethodID child_set_name = env->GetMethodID(child_cls, "setName", 
      "(Ljava/lang/String;)V"); 
    LOGD("testInterface 10"); 

    env->CallVoidMethod(child_obj, child_set_name, str_child); 
    LOGD("testInterface 11"); 

    jmethodID parent_set_child = env->GetMethodID(parent_cls, "setChild", 
      "(Ljava/lang/Object;)V"); 
    LOGD("testInterface 12"); 

    env->CallVoidMethod(parent_obj, parent_set_child, child_obj); 

    LOGD("testInterface 13"); 
    return parent_obj; 
} 

Java:

public class TestParent { 

    private TestChild child; 

    public TestParent(){ 

    } 

    public String getName(){ 
     return this.name; 
    } 

    public void setName(String name){ 
     this.name = name; 
    } 

    public TestChild getChild(){ 
     return this.child; 
    } 

    public void setChild(TestChild child){ 
     this.child = child; 
    } 
} 

public class TestChild { 

    private String name; 

    public TestChild(){ 
    } 

    public String getName(){ 
     return this.name; 
    } 

    public void setName(String name){ 
     this.name = name; 
    } 
} 

ответ

0

Наконец, реализовать эту функцию по-другому:

Java

TestParent parent = new TestParent(); 
TestChild child = new TestChild(); 
parent.setChild(child); 
testInterface2(parent, child); 

JNI

void JNICALL Java_***_testInterface2(JNIEnv * env, jobject thiz, jobject parent_obj, jobject child_obj) { 

    jclass parent_cls = env->GetObjectClass(parent_obj); 

    jstring str_parent = env->NewStringUTF("Parent_from_Native"); 

    jmethodID parent_set_name = env->GetMethodID(parent_cls, "setName", "(Ljava/lang/String;)V"); 

    env->CallVoidMethod(parent_obj, parent_set_name, str_parent); 

    jclass child_cls = env->GetObjectClass(child_obj); 

    jstring str_child = env->NewStringUTF("Child_from_Native"); 

    jmethodID child_set_name = env->GetMethodID(child_cls, "setName", 
      "(Ljava/lang/String;)V"); 

    env->CallVoidMethod(child_obj, child_set_name, str_child); 
}