我試圖將任何活動活動傳遞給用NDK編寫的某些C++代碼。Android JNI:獲取父類
如果例如我傳遞一個MainActivity對象(或可能是SettingsActivity)的引用,如何調用Object類中的getName()方法?
如果我聲明自己的Java方法「giveMeName()」以返回getClass.getName(),那麼giveMeName()可以正常工作,但它當然僅限於我聲明它的類。我如何從C++代碼中調用Object :: getName() - 無需修改Java代碼來傳遞Object引用。
編輯:
我原本以爲它的工作 - 我看到的類名在某個時候返回,但現在它不是。我仍然在嘗試獲得的getName()時返回NULL:
// PDS: Get the class - should be some sort of Activity subclass..
jclass clsActivity = pEnv->GetObjectClass(pObj2);
if(JNIException(pEnv))
return;
LogDebugf("PDS> naNewActivity::clsActivity: %08lx\n", (long) clsActivity);
if(! clsActivity)
return;
jmethodID mid_getClass = pEnv->GetMethodID(clsActivity, "getClass", "()Ljava/lang/Class;");
LogDebugf("PDS> naNewActivity::getClass methodID: %08lx\n", (long) mid_getClass);
if(JNIException(pEnv))
return;
// PDS: Call getClass() method on Activity object (actually Object::getClass())
jclass c = (jclass) pEnv->CallObjectMethod(pObj2, mid_getClass);
LogDebugf("PDS> naNewActivity::getClass() returns: %08lx\n", (long) c);
if(JNIException(pEnv))
return;
// PDS: If we get a Class object, call Class::getName()
jmethodID mid_getName = pEnv->GetMethodID(c, "getName", "()Ljava/lang/String;");
LogDebugf("PDS> naNewActivity::getName methodID: %08lx\n", (long) mid_getName);
*** I AM GETTING null being returned for mid_getName
if(JNIException(pEnv))
return;
// PDS: Call method on Activity object (Object::getName())
jobject n = pEnv->CallObjectMethod(pObj2, mid_getName);
if(JNIException(pEnv))
return;
jstring sName = (jstring) n;
char *pszData = (char*) pEnv->GetStringUTFChars(sName, JNI_FALSE);
LogDebugf("PDS> naNewActivity, class name[%s]\n", pszData);
pEnv->ReleaseStringUTFChars(sName, pszData);
固定碼:我想通了,下面舍瓦的幫助:
// PDS: Get the class - should be some sort of Activity subclass..
jclass clsActivity = pEnv->GetObjectClass(pObj2);
if(JNIException(pEnv))
return;
LogDebugf("PDS> naNewActivity::clsActivity: %08lx\n", (long) clsActivity);
if(! clsActivity)
return;
jmethodID mid_getClass = pEnv->GetMethodID(clsActivity, "getClass", "()Ljava/lang/Class;");
LogDebugf("PDS> naNewActivity::getClass() methodID: %08lx\n", (long) mid_getClass);
if(JNIException(pEnv))
return;
// PDS: Call getClass() method on Activity object.. (actually Object::getClass())
jclass c = (jclass) pEnv->CallObjectMethod(pObj2, mid_getClass);
LogDebugf("PDS> naNewActivity::getClass() returns: %08lx\n", (long) c);
if(JNIException(pEnv))
return;
jclass clsClass = pEnv->GetObjectClass(c);
LogDebugf("PDS> naNewActivity::clsClass: %08lx\n", (long) clsClass);
if(JNIException(pEnv))
return;
// PDS: If we get a Class object, call Class::getName()
jmethodID mid_getName = pEnv->GetMethodID(clsClass, "getName", "()Ljava/lang/String;");
LogDebugf("PDS> naNewActivity::getName() methodID: %08lx\n", (long) mid_getName);
if(JNIException(pEnv))
return;
// PDS: Can't call method on Activity object - need to call it on its Class (object) (Class::getName())
jobject n = pEnv->CallObjectMethod(c, mid_getName);
if(JNIException(pEnv))
return;
jstring sName = (jstring) n;
char *pszData = (char*) pEnv->GetStringUTFChars(sName, JNI_FALSE);
LogDebugf("PDS> naNewActivity, class name[%s]\n", pszData);
pEnv->ReleaseStringUTFChars(sName, pszData);
@Seva Alekseyev:謝謝。我試過「jclass clsObject = pEnv-> FindClass(」java/lang/Object「);」但是返回null。我的語法有什麼問題嗎? – SparkyNZ
看起來很對我。粘貼一些代碼。 –
@Seva Alekseyev:請看看問題中更新的代碼。我確信getName()的方法ID已經返回(它早些時候打印了我的類名),但我不知道我做了什麼來打破它。 – SparkyNZ