@jalomic said:
@a.toraby said:
JavaVM * _jvm = 0;
JNIEnv* _jniENV;
qDebug() << "Attaching current thread";
_jvm->AttachCurrentThread(&_jniENV,NULL);
As i see here _jvm - is a null pointer.. and you call method from null pointer
Yes you are right. I solved it by using a QAndroidJniEnvironment: Here is the working code:
class HelloWorldTask : public QRunnable
{
QAndroidJniEnvironment * _env;
void run()
{
JNIEnv * jniEnv;
JavaVM * jvm = _env->javaVM();
qDebug() << "Getting jni environment";
jvm->GetEnv(reinterpret_cast<void**>(&_env), JNI_VERSION_1_6);
qDebug() << "Attaching current thread";
jvm->AttachCurrentThread(&jniEnv,NULL);
qDebug() << "Creating byte array" ;
jbyteArray jBuffer = jniEnv->NewByteArray(10);
qDebug() << "byte array created" ;
jvm->DetachCurrentThread();
}
public:
void setPointer(QAndroidJniEnvironment * p){
_env = p;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
HelloWorldTask * hello = new HelloWorldTask();
QAndroidJniEnvironment * env;
hello->setPointer(env);
// QThreadPool takes ownership and deletes 'hello' automatically
QThreadPool::globalInstance()->start(hello);
return a.exec();
}