2

我正在尝试从 c>java 调用以打开 URL(即启动 Web 浏览器)。我有在java中工作的代码来启动url(这是我主要活动的一种方法)。当我刚刚在同一个线程上进行调用时它正在工作,但是,因为我已经从另一个线程进行了调用,所以它已经坏了。

我收到以下错误: http: //pastie.org/4696174

09-10 15:59:49.405: W/dalvikvm(31430): JNI WARNING: threadid=11 using env from threadid=1
09-10 15:59:49.405: E/dalvikvm(31430): JNI ERROR: env->self != thread-self (0x12988 vs. 0x200fb0); auto-correcting
09-10 15:59:49.405: W/dalvikvm(31430): JNI WARNING: threadid=11 using env from threadid=1
09-10 15:59:49.405: W/dalvikvm(31430): JNI WARNING: 0x41578910 is not a valid JNI reference
09-10 15:59:49.405: W/dalvikvm(31430):              in Lcom/mrqwak/app/AppRenderer;.onDrawFrameN:()V (CallVoidMethodV)
09-10 15:59:49.415: I/dalvikvm(31430): "GLThread 876" prio=5 tid=11 RUNNABLE
09-10 15:59:49.415: I/dalvikvm(31430):   | group="main" sCount=0 dsCount=0 obj=0x4157b270 self=0x200fb0
09-10 15:59:49.415: I/dalvikvm(31430):   | sysTid=31443 nice=0 sched=0/0 cgrp=default handle=1986128
09-10 15:59:49.420: I/dalvikvm(31430):   | schedstat=( 1035202257 457143963 4029 ) utm=80 stm=23 core=0
09-10 15:59:49.420: I/dalvikvm(31430):   at com.mrqwak.app.AppRenderer.onDrawFrameN(Native Method)
09-10 15:59:49.420: I/dalvikvm(31430):   at com.mrqwak.app.AppRenderer.onDrawFrame(AppActivity.java:289)
09-10 15:59:49.425: I/dalvikvm(31430):   at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1462)
09-10 15:59:49.425: I/dalvikvm(31430):   at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1216)
09-10 15:59:49.425: E/dalvikvm(31430): VM aborting
09-10 15:59:49.430: A/libc(31430): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1)

这是我的 c 代码: http: //pastie.org/4696207

我在主 UI 线程上从 c>java 调用它,因为创建了主要活动:

JNIEnv*     g_envApp            =   0;
jobject     g_objApp            =   0;
jclass      g_classApp          =   0;
jmethodID   g_methodOpenURL     =   0;

JNIEXPORT void JNICALL Java_com_mrqwak_app_AppActivity_onCreateN(JNIEnv *env, jobject obj)
{
    g_envApp = env;
    g_objApp = obj;
    g_classApp = env->GetObjectClass(obj);
    if (g_classApp)
    {
        g_methodOpenURL = env->GetMethodID(g_classApp,"openURL","(Ljava/lang/String;)V");
    }
}

我稍后从 c>java 调用它,而不是在主 UI thead 上,当用户按下按钮打开一个 url 时:

extern  JNIEnv*     g_envApp;
extern  jobject     g_objApp;
extern  jclass      g_classApp;
extern  jmethodID   g_methodOpenURL;

void cHTTP::OpenURL(const char* psURL)
{
    if (g_methodOpenURL)
    {
        jstring jstr = g_envApp->NewStringUTF(psURL);
        g_envApp->CallVoidMethod(g_objApp,g_methodOpenURL,jstr);
    }
}

谢谢,

4

1 回答 1

2

我相信您需要附加到 UI 线程 - jobjects 不能跨线程共享 - 效果如下:

JNIEnv *env;
jvm->AttachCurrentThread(&env, 0);
jmethodID mid = env->GetMethodID(g_classApp, "openURL", "(Ljava/lang/string)V");
env->CallVoidMethod(g_objApp, mid, url);

在您第一次调用 JNI(设置其他变量)时,添加一个 jvm 对象:

JavaVM  *jvm;
...
env->GetJavaVM(&jvm);
于 2012-09-10T16:56:57.777 回答