3

我有以下 JNI 方法,该方法当前调用静态 Java 方法:

void bindToMarketBillingServiceJNI(const char *  inappID)
    {
        JniMethodInfo t;

        if (JniHelper::getStaticMethodInfo(t
            , "com/mycompany/games/js/TestsDemo"
            , "bindToMarketBillingService"
            , "(Ljava/lang/String;)V"))
        {
            jstring stringArg1;

            if (! inappID)
            {
                stringArg1 = t.env->NewStringUTF("1");
            }
            else
            {
                stringArg1 = t.env->NewStringUTF(inappID);
            }

            t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1);

            t.env->DeleteLocalRef(stringArg1);
            t.env->DeleteLocalRef(t.classID);

        }
    }

这是我的Java方法:

public static void bindToMarketBillingService(final String mSku)
    {

// Some code....

}

一切正常。

但是现在,我想让我的 Java 方法成为非静态的。我以为我只需要改变:

  • JniHelper::getStaticMethodInfo 到 JniHelper::getMethodInfo
  • t.env->CallStaticVoidMethod 到 t.env->CallVoidMethod
  • 'public static void bindToMarketBillingService(final String mSku)' 到 'public void bindToMarketBillingService(final String mSku)'

但它崩溃了......(下面的崩溃日志)

有人知道如何解决这个问题吗?

谢谢 !!

08-20 11:54:11.009: A/libc(9379): Fatal signal 11 (SIGSEGV) at 0x0c1f4072 (code=1)
08-20 11:54:11.109: D/dalvikvm(31592): GC_CONCURRENT freed 735K, 12% free 9743K/10951K, paused 4ms+7ms
08-20 11:54:11.184: D/dalvikvm(1976): GC_CONCURRENT freed 1720K, 35% free 28243K/43399K, paused 5ms+13ms
08-20 11:54:11.194: I/ApplicationPolicy(1976): getActualApplicationStateEnabled() : true
08-20 11:54:11.219: D/PJ_MainApplication(9698): onApplicationCreate -> Now
08-20 11:54:11.334: D/dalvikvm(31592): GC_CONCURRENT freed 273K, 9% free 9981K/10951K, paused 3ms+3ms
08-20 11:54:11.409: D/PJ_CrashManager(9698): registerHandler -> Current handler class = com.android.internal.os.RuntimeInit$UncaughtHandler
08-20 11:54:11.469: I/DEBUG(9331): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
08-20 11:54:11.469: I/DEBUG(9331): Build fingerprint: 'samsung/GT-I9100/GT-I9100:4.0.3/IML74K/XWLPG:user/release-keys'
08-20 11:54:11.469: I/DEBUG(9331): pid: 9379, tid: 9403  >>> com.audioguidia.games.js <<<
08-20 11:54:11.469: I/DEBUG(9331): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0c1f4072
08-20 11:54:11.474: I/DEBUG(9331):  r0 00000000  r1 10000000  r2 fffffffc  r3 4cbb9ef0
08-20 11:54:11.474: I/DEBUG(9331):  r4 00273f98  r5 4c7f7fc4  r6 51134ad4  r7 415077f8
08-20 11:54:11.474: I/DEBUG(9331):  r8 00000001  r9 0c1f4071  10 4cbb9ee4  fp 51134a58
08-20 11:54:11.474: I/DEBUG(9331):  ip ffffffec  sp 51134a58  lr 4096864d  pc 40968aac  cpsr 00000030
08-20 11:54:11.474: I/DEBUG(9331):  d0  0000000000000000  d1  3f80000000000000
08-20 11:54:11.474: I/DEBUG(9331):  d2  0000000000000000  d3  0000000000000000
08-20 11:54:11.474: I/DEBUG(9331):  d4  0000000000000000  d5  3f80000000000000
etc....
4

3 回答 3

8

仅更改对env对象的调用是不够的。

首先,您的新实例方法应在 Java 类中声明,例如:(public void myMethod(...)static此处没有关键字)。

其次,您需要一个 java 对象引用来调用实例方法,而不仅仅是类。即你会做类似的事情(请注意我使用的是 C 语法):

jmethodID midCallBack = (*env)->GetMethodID(env, thisClass, "myMethod", "()V");
if (NULL == midCallBack) return;

// Call back the method (which returns void), based on the Method ID
(*env)->CallVoidMethod(env, thisObj, midCallBack);

有关详细信息,请参阅此链接中的“5.3 回调实例方法和静态方法”部分

于 2013-08-20T10:25:55.767 回答
3

如果要调用非静态方法,则必须创建该类的对象,然后仅调用成员方法。代码看起来像这样

jmethodID constructor = env->GetMethodID(cls, "<init>", "()V"); //this is for defaul c-tor
jobject object = env->NewObject(cls, constructor);
//getting your method id
env->CallVoidMethod(object, methodId/*,your parameters*/); 
于 2013-08-21T07:53:54.680 回答
0

我刚刚在java中创建了一个调用非静态方法的静态方法:

static public void startActivity() {
    try {
        Activity mother = QtNative.activity();
        Intent intent = new Intent(mother, MainActivity.class);
        mother.startActivity(intent);
    } catch (Exception e) {
        Log.e(msgTag, e.toString());
        e.printStackTrace();
    }
}

我像这样从 C++ 调用:

void FacebookAndroid::startAndroidFacebook() {
QAndroidJniObject::callStaticMethod<void>("org.qtproject.example.MainActivity",
                                          "startFacebookActivity",
                                          "()V");
}
于 2014-08-13T17:11:35.907 回答