Hello everyone.
I am never programming to Android, this is my first application.
I create simpla application with two threads:
first - Java UI thread, and second thread created by calling native function from Java code.
I want call Java function for print text in TextView, but when I call env->FindClass("test/app/Test"); application did crashed http://img706.imageshack.us/img706/7047/screenshot20100113at145.png.
the AttachCurrentThread return 0.
this is example of my native code:
I am never programming to Android, this is my first application.
I create simpla application with two threads:
first - Java UI thread, and second thread created by calling native function from Java code.
I want call Java function for print text in TextView, but when I call env->FindClass("test/app/Test"); application did crashed http://img706.imageshack.us/img706/7047/screenshot20100113at145.png.
the AttachCurrentThread return 0.
this is example of my native code:
Code:
#include "test.h"
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
JavaVM* g_jvm = 0;
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void * reserved)
{
g_jvm = vm;
return JNI_VERSION_1_6;
}
void JNI_OnUnload(JavaVM *vm, void *reserved)
{
g_jvm = 0;
}
void* threadProc(void*)
{
JNIEnv* env = 0;
int res = g_jvm->AttachCurrentThread(&env, NULL); // res == 0
jclass jclsTest = env->FindClass("test/app/Test"); // if comment this line, application not crashed
g_jvm->DetachCurrentThread();
env = 0;
return 0;
}
JNIEXPORT jboolean JNICALL Java_test_app_Test_initialize(JNIEnv* env, jobject obj)
{
jclass jcls = env->FindClass("test/app/Test");
jmethodID mid = env->GetMethodID(jcls, "messageOutput", "(Ljava/lang/String;)V");
env->CallVoidMethod(obj, mid, env->NewStringUTF("start thread"));
pthread_t thread = 0;
pthread_create(&thread, NULL, threadProc, NULL);
env->CallVoidMethod(obj, mid, env->NewStringUTF("thread started"));
return true;
}
Don't assign scalars to pointers...