Android jni JNI_OnLoad error -
apiaccess.h
#include <jni.h> #ifndef apiaccess_h #define apiaccess_h void init_data(jnienv* env); void toast(char* msg); #endif
hello-jni.c
#include <string.h> #include <jni.h> #include "apiaccess.h" jstring javastr( jnienv* env, jobject thiz ); void connectfunc(jnienv* env){ jninativemethod methods[] = {{ "stringfromjni", "()ljava/lang/string;", (void*)javastr}}; jclass clazz = (*env)->findclass(env, "com/testndk/ndk/hellojni"); int status = ((*env)->registernatives(env, clazz, methods, 1)); } jint jni_onload(javavm *vm, void *reserved){ // jni env function calls jnienv* env = null; if ((*vm)->getenv(vm, (void**)&env, jni_version_1_6) != jni_ok) return -1; connectfunc(env); init_data(env); toast("toast in jni_onload"); //error //return jni version need. default valur jni 1.1 return jni_version_1_6; } jstring javastr( jnienv* env, jobject thiz ){ toast("toast in javastr");//no error return (*env)->newstringutf(env, "hello jni !"); }
apiaccess.c
#include <jni.h> jnienv* env = 0; void init_data(jnienv* parenv){ env = parenv; } void toast(char* msg){ static jclass clazz = 0; if(clazz==0) clazz = (*env)->findclass(env, "com/testndk/ndk/hellojni"); static jmethodid method = 0; if(method==0) method = (*env)->getstaticmethodid(env, clazz, "toast", "(ljava/lang/string;)v"); (*env)->callstaticvoidmethod(env,clazz,method,(*env)->newstringutf(env, msg)); }
hellojni.java
package com.testndk.ndk; import android.app.*; import android.content.*; import android.os.*; import android.widget.*; import android.util.*; public class hellojni extends activity{ private static context ctx; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); hellojni.ctx = getapplicationcontext(); textview tv = new textview(this); tv.settext(stringfromjni()); setcontentview(tv); } public static void toast(string msg){ toast.maketext(hellojni.ctx, msg, toast.length_short).show(); } public native string stringfromjni(); static { system.loadlibrary("hello-jni"); } }
calling toast in javastr works fine. in jni_onload, crashes application. why can't call toast in jni_onload()? provided code related error. logcat output:
i/timeline( 1558): timeline: activity_launch_request id:com.testndk.ndk time:14821530 v/applicationpolicy( 999): isapplicationstateblocked userid 0 pkgname com.testndk.ndk d/resourcesmanager( 999): creating new assetmanager , set /data/app/com.testndk.ndk-1/base.apk i/activitymanager( 999): start u0 {act=android.intent.action.main cat=[android.intent.category.launcher] flg=0x10200000 cmp=com.testndk.ndk/.hellojni} uid 10008 on display 0 i/activitymanager( 999): start proc com.testndk.ndk activity com.testndk.ndk/.hellojni: pid=28993 uid=10281 gids={50281, 9997} abi=armeabi i/surfaceflinger( 246): id=6131 createsurf (1080x1920),2 flag=404, starting com.testndk.ndk d/resourcesmanager(28993): creating new assetmanager , set /data/app/com.testndk.ndk-1/base.apk e/androidruntime(28993): process: com.testndk.ndk, pid: 28993 e/androidruntime(28993): @ com.testndk.ndk.hellojni.toast(hellojni.java:25) e/androidruntime(28993): @ com.testndk.ndk.hellojni.<clinit>(hellojni.java:30) v/applicationpolicy( 999): isapplicationstateblocked userid 0 pkgname com.testndk.ndk w/activitymanager( 999): force finishing activity com.testndk.ndk/.hellojni d/crashanrdetector( 999): processname: com.testndk.ndk d/crashanrdetector( 999): broadcastevent : com.testndk.ndk data_app_crash i/surfaceflinger( 246): id=6132 createsurf (49x49),1 flag=4, application error: com.testndk.ndk w/activitymanager( 999): activity pause timeout activityrecord{3a27a7c6 u0 com.testndk.ndk/.hellojni t1692 f} i/surfaceflinger( 246): id=6131 removed starting com.testndk.ndk (6/10) i/surfaceflinger( 246): id=6131 removed starting com.testndk.ndk (-2/10) i/activitymanager( 999): process com.testndk.ndk (pid 28993)(adj 9) has died(65,325) i/surfaceflinger( 246): id=6132 removed application error: com.testndk.ndk (7/9) i/surfaceflinger( 246): id=6132 removed application error: com.testndk.ndk (-2/9) d/networkstatsfactory( 999): updatestatsforknox
edit see problem is. problem jni_onload call on system.loadlibrary , static block call when class loads. ctx not initialized , caused nullpointerexception not realized static block runs earlier constructor
Comments
Post a Comment