使用JNI崩溃将字符串从C ++传递到Java(Android)

时间:2013-12-16 07:10:37

标签: java android c++ crash java-native-interface

我在尝试将字符串从C ++传递到Java时遇到了问题。这是C ++方面的代码:

void AndroidJNIInterfacer::callJavaVoidMethod(const char* name, const char* sig, const char* char1)
{
    jmethodID method = getJavaMethod(name, sig);
    jstring argString = env->NewStringUTF(char1);

    if (method != NULL)
        env->CallVoidMethod(nativeActivity, method, argString);

    jvm->GetEnv((void**) &env, version);
}

这是Java方面的代码:

public void showLeaderboardActivity(final String id)
    {
        //int REQUEST_LEADERBOARD = 100;
        Log.d("MyNativeActivity", "Before id");
        Log.d("MyNativeActivity", id);
        //startActivityForResult(getGamesClient().getLeaderboardIntent(id), REQUEST_LEADERBOARD);
    }

这是logcat输出:

12-16 01:57:58.697: D/MyNativeActivity(22071): Before id
12-16 01:57:58.721: I/dalvikvm(22071): threadid=4: reacting to signal 3
12-16 01:57:59.721: W/dalvikvm(22071): threadid=4: spin on suspend #1 threadid=1 (pcf=0)
12-16 01:58:00.471: W/dalvikvm(22071): threadid=4: spin on suspend #2 threadid=1 (pcf=0)
12-16 01:58:00.475: I/dalvikvm(22071): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
12-16 01:58:00.475: I/dalvikvm(22071):   | group="system" sCount=0 dsCount=0 obj=0x4050db20 self=0x117d60
12-16 01:58:00.475: I/dalvikvm(22071):   | sysTid=22074 nice=0 sched=0/0 cgrp=default handle=582800
12-16 01:58:00.475: I/dalvikvm(22071):   at dalvik.system.NativeStart.run(Native Method)
12-16 01:58:00.475: I/dalvikvm(22071): "main" prio=5 tid=1 RUNNABLE
12-16 01:58:00.475: I/dalvikvm(22071):   | group="main" sCount=1 dsCount=0 obj=0x4001f1b0 self=0xce60
12-16 01:58:00.475: I/dalvikvm(22071):   | sysTid=22071 nice=0 sched=0/0 cgrp=default handle=-1345006496
12-16 01:58:00.475: I/dalvikvm(22071):   at android.util.Log.println_native(Native Method)
12-16 01:58:00.475: I/dalvikvm(22071):   at android.util.Log.d(Log.java:137)
12-16 01:58:00.479: I/dalvikvm(22071):   at com.madstermadster.myNativeActivity.MyNativeActivity$1.run(MyNativeActivity.java:46)
12-16 01:58:00.483: I/dalvikvm(22071):   at android.os.Handler.handleCallback(Handler.java:587)
12-16 01:58:00.483: I/dalvikvm(22071):   at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 01:58:00.483: I/dalvikvm(22071):   at android.os.Looper.loop(Looper.java:130)
12-16 01:58:00.490: I/dalvikvm(22071):   at android.app.ActivityThread.main(ActivityThread.java:3687)
12-16 01:58:00.490: I/dalvikvm(22071):   at java.lang.reflect.Method.invokeNative(Native Method)
12-16 01:58:00.494: I/dalvikvm(22071):   at java.lang.reflect.Method.invoke(Method.java:507)
12-16 01:58:00.494: I/dalvikvm(22071):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-16 01:58:00.494: I/dalvikvm(22071):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-16 01:58:00.494: I/dalvikvm(22071):   at dalvik.system.NativeStart.main(Native Method)
12-16 01:58:01.244: W/dalvikvm(22071): threadid=4: spin on suspend #3 threadid=1 (pcf=0)
12-16 01:58:01.244: I/dalvikvm(22071): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
12-16 01:58:01.248: I/dalvikvm(22071):   | group="system" sCount=0 dsCount=0 obj=0x4050db20 self=0x117d60
12-16 01:58:01.248: I/dalvikvm(22071):   | sysTid=22074 nice=0 sched=0/0 cgrp=default handle=582800
12-16 01:58:01.248: I/dalvikvm(22071):   at dalvik.system.NativeStart.run(Native Method)
12-16 01:58:01.248: I/dalvikvm(22071): "main" prio=5 tid=1 RUNNABLE
12-16 01:58:01.248: I/dalvikvm(22071):   | group="main" sCount=1 dsCount=0 obj=0x4001f1b0 self=0xce60
12-16 01:58:01.256: I/dalvikvm(22071):   | sysTid=22071 nice=0 sched=0/0 cgrp=default handle=-1345006496
12-16 01:58:01.459: I/dalvikvm(22071):   at android.util.Log.println_native(Native Method)
12-16 01:58:01.459: I/dalvikvm(22071):   at android.util.Log.d(Log.java:137)
12-16 01:58:01.561: I/dalvikvm(22071):   at com.madstermadster.myNativeActivity.MyNativeActivity$1.run(MyNativeActivity.java:46)
12-16 01:58:01.561: I/dalvikvm(22071):   at android.os.Handler.handleCallback(Handler.java:587)
12-16 01:58:01.561: I/dalvikvm(22071):   at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 01:58:01.561: I/dalvikvm(22071):   at android.os.Looper.loop(Looper.java:130)
12-16 01:58:01.561: I/dalvikvm(22071):   at android.app.ActivityThread.main(ActivityThread.java:3687)
12-16 01:58:01.561: I/dalvikvm(22071):   at java.lang.reflect.Method.invokeNative(Native Method)
12-16 01:58:01.561: I/dalvikvm(22071):   at java.lang.reflect.Method.invoke(Method.java:507)
12-16 01:58:01.561: I/dalvikvm(22071):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-16 01:58:01.561: I/dalvikvm(22071):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-16 01:58:01.561: I/dalvikvm(22071):   at dalvik.system.NativeStart.main(Native Method)
12-16 01:58:02.311: W/dalvikvm(22071): threadid=4: spin on suspend #4 threadid=1 (pcf=0)
12-16 01:58:02.311: I/dalvikvm(22071): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
12-16 01:58:02.311: I/dalvikvm(22071):   | group="system" sCount=0 dsCount=0 obj=0x4050db20 self=0x117d60
12-16 01:58:02.311: I/dalvikvm(22071):   | sysTid=22074 nice=0 sched=0/0 cgrp=default handle=582800
12-16 01:58:02.311: I/dalvikvm(22071):   at dalvik.system.NativeStart.run(Native Method)
12-16 01:58:02.311: I/dalvikvm(22071): "main" prio=5 tid=1 RUNNABLE
12-16 01:58:02.311: I/dalvikvm(22071):   | group="main" sCount=1 dsCount=0 obj=0x4001f1b0 self=0xce60
12-16 01:58:02.311: I/dalvikvm(22071):   | sysTid=22071 nice=0 sched=0/0 cgrp=default handle=-1345006496
12-16 01:58:02.686: I/dalvikvm(22071):   at android.util.Log.println_native(Native Method)
12-16 01:58:02.725: I/dalvikvm(22071):   at android.util.Log.d(Log.java:137)
12-16 01:58:02.775: I/dalvikvm(22071):   at com.madstermadster.myNativeActivity.MyNativeActivity$1.run(MyNativeActivity.java:46)
12-16 01:58:02.803: I/dalvikvm(22071):   at android.os.Handler.handleCallback(Handler.java:587)
12-16 01:58:02.822: I/dalvikvm(22071):   at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 01:58:02.854: I/dalvikvm(22071):   at android.os.Looper.loop(Looper.java:130)
12-16 01:58:02.889: I/dalvikvm(22071):   at android.app.ActivityThread.main(ActivityThread.java:3687)
12-16 01:58:02.889: I/dalvikvm(22071):   at java.lang.reflect.Method.invokeNative(Native Method)
12-16 01:58:02.900: I/dalvikvm(22071):   at java.lang.reflect.Method.invoke(Method.java:507)
12-16 01:58:02.900: I/dalvikvm(22071):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-16 01:58:02.900: I/dalvikvm(22071):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-16 01:58:02.900: I/dalvikvm(22071):   at dalvik.system.NativeStart.main(Native Method)
12-16 01:58:03.650: W/dalvikvm(22071): threadid=4: spin on suspend #5 threadid=1 (pcf=0)
12-16 01:58:03.650: I/dalvikvm(22071): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
12-16 01:58:03.650: I/dalvikvm(22071):   | group="system" sCount=0 dsCount=0 obj=0x4050db20 self=0x117d60
12-16 01:58:03.650: I/dalvikvm(22071):   | sysTid=22074 nice=0 sched=0/0 cgrp=default handle=582800
12-16 01:58:03.650: I/dalvikvm(22071):   at dalvik.system.NativeStart.run(Native Method)
12-16 01:58:03.650: I/dalvikvm(22071): "main" prio=5 tid=1 RUNNABLE
12-16 01:58:03.650: I/dalvikvm(22071):   | group="main" sCount=1 dsCount=0 obj=0x4001f1b0 self=0xce60
12-16 01:58:03.650: I/dalvikvm(22071):   | sysTid=22071 nice=0 sched=0/0 cgrp=default handle=-1345006496
12-16 01:58:03.654: I/dalvikvm(22071):   at android.util.Log.println_native(Native Method)
12-16 01:58:03.654: I/dalvikvm(22071):   at android.util.Log.d(Log.java:137)
12-16 01:58:03.654: I/dalvikvm(22071):   at com.madstermadster.myNativeActivity.MyNativeActivity$1.run(MyNativeActivity.java:46)
12-16 01:58:03.654: I/dalvikvm(22071):   at android.os.Handler.handleCallback(Handler.java:587)
12-16 01:58:03.654: I/dalvikvm(22071):   at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 01:58:03.654: I/dalvikvm(22071):   at android.os.Looper.loop(Looper.java:130)
12-16 01:58:03.654: I/dalvikvm(22071):   at android.app.ActivityThread.main(ActivityThread.java:3687)
12-16 01:58:03.658: I/dalvikvm(22071):   at java.lang.reflect.Method.invokeNative(Native Method)
12-16 01:58:03.658: I/dalvikvm(22071):   at java.lang.reflect.Method.invoke(Method.java:507)
12-16 01:58:03.658: I/dalvikvm(22071):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-16 01:58:03.658: I/dalvikvm(22071):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-16 01:58:03.658: I/dalvikvm(22071):   at dalvik.system.NativeStart.main(Native Method)
12-16 01:58:04.408: W/dalvikvm(22071): threadid=4: spin on suspend #6 threadid=1 (pcf=0)
12-16 01:58:04.408: I/dalvikvm(22071): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
12-16 01:58:04.408: I/dalvikvm(22071):   | group="system" sCount=0 dsCount=0 obj=0x4050db20 self=0x117d60
12-16 01:58:04.408: I/dalvikvm(22071):   | sysTid=22074 nice=0 sched=0/0 cgrp=default handle=582800
12-16 01:58:04.408: I/dalvikvm(22071):   at dalvik.system.NativeStart.run(Native Method)
12-16 01:58:04.408: I/dalvikvm(22071): "main" prio=5 tid=1 RUNNABLE
12-16 01:58:04.408: I/dalvikvm(22071):   | group="main" sCount=1 dsCount=0 obj=0x4001f1b0 self=0xce60
12-16 01:58:04.408: I/dalvikvm(22071):   | sysTid=22071 nice=0 sched=0/0 cgrp=default handle=-1345006496
12-16 01:58:04.408: I/dalvikvm(22071):   at android.util.Log.println_native(Native Method)
12-16 01:58:04.408: I/dalvikvm(22071):   at android.util.Log.d(Log.java:137)
12-16 01:58:04.408: I/dalvikvm(22071):   at com.madstermadster.myNativeActivity.MyNativeActivity$1.run(MyNativeActivity.java:46)
12-16 01:58:04.408: I/dalvikvm(22071):   at android.os.Handler.handleCallback(Handler.java:587)
12-16 01:58:04.408: I/dalvikvm(22071):   at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 01:58:04.408: I/dalvikvm(22071):   at android.os.Looper.loop(Looper.java:130)
12-16 01:58:04.408: I/dalvikvm(22071):   at android.app.ActivityThread.main(ActivityThread.java:3687)
12-16 01:58:04.408: I/dalvikvm(22071):   at java.lang.reflect.Method.invokeNative(Native Method)
12-16 01:58:04.408: I/dalvikvm(22071):   at java.lang.reflect.Method.invoke(Method.java:507)
12-16 01:58:04.408: I/dalvikvm(22071):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-16 01:58:04.408: I/dalvikvm(22071):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-16 01:58:04.408: I/dalvikvm(22071):   at dalvik.system.NativeStart.main(Native Method)
12-16 01:58:05.158: W/dalvikvm(22071): threadid=4: spin on suspend #7 threadid=1 (pcf=0)
12-16 01:58:05.264: I/dalvikvm(22071): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
12-16 01:58:05.264: I/dalvikvm(22071):   | group="system" sCount=0 dsCount=0 obj=0x4050db20 self=0x117d60
12-16 01:58:05.264: I/dalvikvm(22071):   | sysTid=22074 nice=0 sched=0/0 cgrp=default handle=582800
12-16 01:58:05.264: I/dalvikvm(22071):   at dalvik.system.NativeStart.run(Native Method)
12-16 01:58:05.264: I/dalvikvm(22071): "main" prio=5 tid=1 RUNNABLE
12-16 01:58:05.264: I/dalvikvm(22071):   | group="main" sCount=1 dsCount=0 obj=0x4001f1b0 self=0xce60
12-16 01:58:05.264: I/dalvikvm(22071):   | sysTid=22071 nice=0 sched=0/0 cgrp=default handle=-1345006496
12-16 01:58:05.268: I/dalvikvm(22071):   at android.util.Log.println_native(Native Method)
12-16 01:58:05.350: I/dalvikvm(22071):   at android.util.Log.d(Log.java:137)
12-16 01:58:05.358: I/dalvikvm(22071):   at com.madstermadster.myNativeActivity.MyNativeActivity$1.run(MyNativeActivity.java:46)
12-16 01:58:05.358: I/dalvikvm(22071):   at android.os.Handler.handleCallback(Handler.java:587)
12-16 01:58:05.358: I/dalvikvm(22071):   at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 01:58:05.361: I/dalvikvm(22071):   at android.os.Looper.loop(Looper.java:130)
12-16 01:58:05.365: I/dalvikvm(22071):   at android.app.ActivityThread.main(ActivityThread.java:3687)
12-16 01:58:05.365: I/dalvikvm(22071):   at java.lang.reflect.Method.invokeNative(Native Method)
12-16 01:58:05.373: I/dalvikvm(22071):   at java.lang.reflect.Method.invoke(Method.java:507)
12-16 01:58:05.373: I/dalvikvm(22071):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-16 01:58:05.373: I/dalvikvm(22071):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-16 01:58:05.373: I/dalvikvm(22071):   at dalvik.system.NativeStart.main(Native Method)

我的初始LOGI似乎有效,所以很明显正确找到了java方法。当我引用我传递它的id时,它会崩溃,所以我怀疑问题在于我如何转换和/或传递字符串。任何帮助将不胜感激,当然,我很乐意发布任何其他代码/答案的任何问题。谢谢!

编辑:env变量由函数setEnv()设置,该函数在任何其他调用之前调用:

void setEnv()
{
    jvm->GetEnv((void**) &env, version);
}

jvm是在程序的最开始设置的。我相信这个和getJavaMethod都能正常工作,因为我可以在没有参数的情况下调用Java方法。

编辑2:根据汤姆的一些建议(见下文),我现在有以下内容:

void AndroidJNIInterfacer::callJavaVoidMethod(const char* name, const char* sig, const char* char1)
{
    jmethodID method = getJavaMethod(name, sig);
    jstring argString = env->NewStringUTF(char1);

    const jchar* result = env->GetStringChars(argString, JNI_FALSE);

    if (method != NULL)
        env->CallVoidMethod(nativeActivity, method, result);

    env->DeleteLocalRef(argString);
}

public void showLeaderboardActivity(byte[] id)
    {
        //int REQUEST_LEADERBOARD = 100;
        String sId = String(id);

        Log.d("MyNativeActivity", "Before id");
        Log.d("MyNativeActivity", sId);
        Log.d("MyNativeActivity", "After id");
        //startActivityForResult(getGamesClient().getLeaderboardIntent(id), REQUEST_LEADERBOARD);
    }

至少不再完全崩溃,但仅输出“之前的ID”。我很确定我实际上并没有获得jbytearray,但我不确定如何准确地获得一个jbytearray。

编辑:解决了!事实证明,将const char *从C ++传递给Java是完全正常的,就像我最初做的那样。问题是我犯了一个愚蠢的错误,导致调用了错误版本的重载函数。没有为我的id参数设置值,导致崩溃。因此,如果您有类似的错误,您可能使用错误的参数数量调用CallVoidMethod(或类似的函数)。

0 个答案:

没有答案