gemini.plus0612
unread,Sep 19, 2011, 3:39:20 AM9/19/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to android-ndk
Hi all, I have a problem when I try to getAssets() in my native thread
callback function.
It always got an exception "java.lang.NullPointerException" on code
"this.context.getAssets()" in JAVA side.
Can anyone help me to solve this problem?
Thank you.
C++ Side:
void* test_callback_function(void* ptr){
JNIEnv *env = NULL;
int status=-1;
bool isAttached = false;
status = g_vm->GetEnv(reinterpret_cast<void**>(&env),
JNI_VERSION_1_4);
if(status < 0) {
status = g_vm->AttachCurrentThread(&env, NULL);
if(status < 0) {
return NULL;
}
isAttached = true;
}
jclass interfaceClass;
if(isAttached){
interfaceClass = env->GetObjectClass(gInterfaceObject);
g_this = gInterfaceObject;
}
else{
interfaceClass = env->GetObjectClass(g_this);
}
if(!interfaceClass) {
if(isAttached) g_vm->DetachCurrentThread();
return NULL;
}
// Find the callBack method ID
jmethodID method;
method = env->GetMethodID(interfaceClass, "getAssetBytes", "(Ljava/
lang/String;)[B");
if(!method) {
if(isAttached) g_vm->DetachCurrentThread();
return NULL;
}
//jstring jAssetName = env->NewStringUTF(assetName);
jstring jAssetName = env->NewStringUTF("myassets/test.jpg");
if(jAssetName == NULL) {
if(isAttached) g_vm->DetachCurrentThread();
return NULL;
}
//- Call JAVA Method
jbyteArray jBytes;
jBytes = (jbyteArray)env->CallObjectMethod(g_this, method,
jAssetName);
if(jBytes == NULL) {
if(isAttached) g_vm->DetachCurrentThread();
return NULL;
}
int len = env->GetArrayLength(jBytes);
if(len > 0) {
*buffer = (unsigned char *)malloc(len);
env->GetByteArrayRegion(jBytes, 0, len, (signed char
*)*buffer);
if(isAttached) g_vm->DetachCurrentThread();
return len;
}
if(isAttached) g_vm->DetachCurrentThread();
return NULL;
}
JNIEXPORT int JNICALL do_my_thread(JNIEnv *env, jobject obj)
{
pthread_t thread1;
pthread_create(&thread1, NULL, test_callback_function, (void*)env);
}
JAVA side:
public byte[] getAssetBytes(String assetName) {
AssetManager assets = this.context.getAssets();
InputStream asset = assets.open(assetName);
....
}