我试图将任何活动的活动传递给使用NDK编写的某些C ++代码。
例如,如果我传递对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 );
固定代码:我在下面找到了Seva的帮助:
// 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 );
答案 0 :(得分:1)
您不需要修改代码。如果该方法完全可访问,则调用它的代码与在派生类中声明方法时使用的代码相同。
答案 1 :(得分:0)
使用java.lang.Object
类查找方法ID,然后使用派生类实例的对象指针来调用方法。 JNI允许这样做。首先,您必须按名称找到java.lang.Object
的班级ID。