我成功走原生调用java方法的流程,但是当java方法包含ui方法时,ui方法总是失败,logcat会打印VM aborting错误:
E/ListenerTest( 1399): virtual void JNIListenerContext::postEv(int32_t)
W/dalvikvm( 1399): JNI WARNING: JNI method called with exception pending
W/dalvikvm( 1399): in Ldalvik/system/NativeStart;.run:()V (CallStaticVoidMethodV)
W/dalvikvm( 1399): Pending exception is:
I/dalvikvm( 1399): "Binder Thread #2" prio=5 tid=10 NATIVE
I/dalvikvm( 1399): | group="main" sCount=0 dsCount=0 obj=0x413f0b98 self=0x89b7e8
I/dalvikvm( 1399): | sysTid=1411 nice=0 sched=0/0 cgrp=default handle=8998000
I/dalvikvm( 1399): | schedstat=( 0 0 0 ) utm=0 stm=0 core=0
I/dalvikvm( 1399): at dalvik.system.NativeStart.run(Native Method)
I/dalvikvm( 1399):
E/dalvikvm( 1399): VM aborting
在我的本机代码中:
JNIEnv *env = AndroidRuntime::getJNIEnv();
env->CallStaticVoidMethod( jclass, jmethod, arg);// call java method postEv(arg)
在我的java代码中:
class ListenerClass {
... ...
private static Listener mListener = null;
public interface Listener {
public void onMyListener( int arg );
}
public void registerListener (Listener listener) {
Log.d( TAG,"registerListener");
mListener = listener;
}
public static void postEv( int arg ) {
Log.d( TAG,"postEv");
if( mListener != null ) {
mListener.onMyListener( arg );
} else {
Log.d( TAG,"no Listener");
}
}
}
public class ListenerDemo extends Activity implements ListenerClass.Listener
... ...
private TextView info;
private ListenerClass lisener = null;
public void onCreate(Bundle savedInstanceState) {
info = (TextView) findViewById(R.id.info);
info.setTextColor(Color.WHITE);
info.setTextSize(20);
info.setText("haha"); // this works
lisener = new ListenerClass();
Log.d( TAG,"lisener.registerListener");
lisener.registerListener(this);
}
public void onMyListener( int arg ) {
Log.d(TAG,"arg is: " + arg ); // this works
info.setText("haha"); // this not works
}
... ...
}
我真的不知道为什么会这样。当一个事件推送到 java 的 onMyListener 时,我希望 TextView 显示。