我正在尝试从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> gt java中调用此函数,而不是在主UI上,当用户按下按钮打开网址时
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);
}
}
谢谢,
答案 0 :(得分:1)
我相信你需要附加到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);