Android JNI:获取父类



我正试图将任何活动传递给一些用NDK编写的c++代码。

例如,如果我传递一个引用到MainActivity对象,(或者可能是SettingsActivity),我怎么能调用一个方法,如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: %08lxn", (long) clsActivity );
    if( ! clsActivity )
      return;
    jmethodID mid_getClass = pEnv->GetMethodID( clsActivity, "getClass", "()Ljava/lang/Class;");
    LogDebugf( "PDS> naNewActivity::getClass methodID: %08lxn", (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: %08lxn", (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: %08lxn", (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: %08lxn", (long) clsActivity );
if( ! clsActivity )
  return;
jmethodID mid_getClass = pEnv->GetMethodID( clsActivity, "getClass", "()Ljava/lang/Class;");
LogDebugf( "PDS> naNewActivity::getClass() methodID: %08lxn", (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: %08lxn", (long) c );
if( JNIException( pEnv ) )
  return;
jclass clsClass   = pEnv->GetObjectClass( c );
LogDebugf( "PDS> naNewActivity::clsClass: %08lxn", (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: %08lxn", (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 );

您不需要修改代码。如果方法是可访问的,那么调用它的代码与在派生类中声明该方法时使用的代码是相同的。

您使用java.lang.Object类查找方法ID,然后使用派生类实例的对象指针调用该方法。JNI允许这样做。首先,您必须通过名称找到java.lang.Object的类ID。

相关内容

  • 没有找到相关文章