问题描述
- 安卓 JNI回调Java 总是崩溃
-
java 代码:package com.example.jnitest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;import java.util.Date;
public class MainActivity extends Activity {
private TextView mTextView; private Button mCheckBtn; public native String displayHelloWorld();// java native方法申明 public native void callBack();// java native方法申明 public native void sayHello();// java native方法申明 static { System.loadLibrary("JNICall");// 装入动态链接库 } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.test); mTextView.setText(displayHelloWorld()); mCheckBtn = (Button) findViewById(R.id.check); mCheckBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { callBack(); } }); } public void showMessageFromNative(String message) { System.out.println("chlu---------message=" + message); } public int signTest(int i, Date date, int[] arr) { System.out.println("chlu--------- Sign Test"); return 0; }
}
C++代码:
#include
#include "JNICall.h"
JNIEnv* env;
JavaVM *g_jm;
JNIEXPORT jstring JNICALL Java_com_example_jnitest_MainActivity_displayHelloWorld(
JNIEnv *env, jobject thiz) {
return env->NewStringUTF("chlu---Hello from JNI !");
}JNIEXPORT void JNICALL Java_com_example_jnitest_MainActivity_callBack(
JNIEnv* env, jobject obj) {
const char *message = "C++传到上层JAVA"; //需要传递到Java层的字符串
jclass class_z = env->FindClass("com/example/jnitest/MainActivity");
jmethodID className_method = env->GetMethodID(class_z,
"showMessageFromNative", "(Ljava/lang/String;)V");
env->CallVoidMethod(obj, className_method, message);}
JNIEXPORT void JNICALL Java_com_example_jnitest_MainActivity_sayHello(
JNIEnv * env, jobject obj) {//否则就传入一个jclass对象表示native()方法所在的类 jclass native_clazz = env->GetObjectClass(obj); //得到jmethodID jmethodID methodID_func = env->GetMethodID(native_clazz, "signTest", "(ILjava/util/Date;[I)I"); //调用signTest方法 env->CallIntMethod(obj, methodID_func, 1L, NULL, NULL);
}
jint JNI_OnLoad(JavaVM* vm, void* reserved){
jint result = -1;
g_jm = vm;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
return result;
}
if (env == NULL) {
return result;
}
result = JNI_VERSION_1_4;
return result;
}/* DO NOT EDIT THIS FILE - it is machine generated /
#include
#include
#include
/ Header for class HelloWorld */#ifndef _Included_JNITCall
#define _Included_JNITCall
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jstring JNICALL Java_com_example_jnitest_MainActivity_displayHelloWorld(
JNIEnv env, jobject thiz);
JNIEXPORT void JNICALL Java_com_example_jnitest_MainActivity_callBack(
JNIEnv env, jobject obj);
JNIEXPORT void JNICALL Java_com_example_jnitest_MainActivity_sayHello(
JNIEnv * env, jobject obj);
#ifdef __cplusplus
}
#endif
#endif错误:
/libdvm.so. Error (3): No such process
Stack frame #15 pc 000297e0 /system/lib/libdvm.so: Unable to open symbol file /obj/local/armeabi/libdvm.so. Error (3): No such process
Stack frame #16 pc 00030c6c /system/lib/libdvm.so (dvmMterpStd(Thread*)+76): Unable to open symbol file /obj/local/armeabi/libdvm.so. Error (3): No such process
Stack frame #17 pc 0002e304 /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+184): Unable to open symbol file /obj/local/armeabi/libdvm.so. Error (3): No such process
Stack frame #18 pc 00063715 /system/lib/libdvm.so (dvmInvokeMethod(Object*, Method const*, ArrayObject*, ArrayObject*, ClassObject*, bool)+392): Unable to open symbol file /obj/local/armeabi/libdvm.so. Error (3): No such process
Stack frame #19 pc 0006b627 /system/lib/libdvm.so: Unable to open symbol file /obj/local/armeabi/libdvm.so. Error (3): No such process
Stack frame #20 pc 000297e0 /system/lib/libdvm.so: Unable to open symbol file /obj/local/armeabi/libdvm.so. Error (3): No such process
Stack frame #21 pc 00030c6c /system/lib/libdvm.so (dvmMterpStd(Thread*)+76): Unable to open symbol file /obj/local/armeabi/libdvm.so. Error (3): No such process
Stack frame #22 pc 0002e304 /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+184): Unable to open symbol file /obj/local/armeabi/libdvm.so. Error (3): No such process
Stack frame #23 pc 00063431 /system/lib/libdvm.so (dvmCallMethodV(Thread*, Method const*, Object*, bool, JValue*, std::__va_list)+336): Unable to open symbol file /obj/local/armeabi/libdvm.so. Error (3): No such process
Stack frame #24 pc 0004cbb7 /system/lib/libdvm.so: Unable to open symbol file /obj/local/armeabi/libdvm.so. Error (3): No such process
Stack frame #25 pc 0004e32b /system/lib/libandroid_runtime.so: Unable to open symbol file /obj/local/armeabi/libandroid_runtime.so. Error (3): No such process
Stack frame #26 pc 0004f057 /system/lib/libandroid_runtime.so (android::AndroidRuntime::start(char const*, char const*, bool)+358): Unable to open symbol file /obj/local/armeabi/libandroid_runtime.so. Error (3): No such process
Stack frame #27 pc 00001063 /system/bin/app_process: Unable to open symbol file /obj/local/armeabi/app_process. Error (3): No such process
Stack frame #28 pc 0000e4c3 /system/lib/libc.so (__libc_init+50): Unable to open symbol file /obj/local/armeabi/libc.so. Error (3): No such process
Stack frame #29 pc 00000d80 /system/bin/app_process: Unable to open symbol file /obj/local/armeabi/app_process. Error (3): No such process
Crash dump is completed********** Crash dump: **********
Build fingerprint: 'Xiaomi/virgo/virgo:4.4.4/KTU84P/5.11.12:user/release-keys'
pid: 23900, tid: 23900, name: example.jnitest >>> com.example.jnitest <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Stack frame #00 pc 0002210c /system/lib/libc.so (tgkill+12): Unable to open symbol file /obj/local/armeabi/libc.so. Error (3): No such process
Stack frame #01 pc 00013165 /system/lib/libc.so (pthread_kill+48): Unable to open symbol file /obj/local/armeabi/libc.so. Error (3): No such process
Stack frame #02 pc 00013379 /system/lib/libc.so (raise+10): Unable to open symbol file /obj/local/armeabi/libc.so. Error (3): No such process
Stack frame #03 pc 000120a3 /system/lib/libc.so: Unable to open symbol file /obj/local/armeabi/libc.so. Error (3): No such process
Stack frame #04 pc 000219c0 /system/lib/libc.so (abort+4): Unable to open symbol file /obj/local/armeabi/libc.so. Error (3): No such process
Stack frame #05 pc 00048b1f /system/lib/libdvm.so (dvmAbort+78): Unable to open symbol file /obj/local/armeabi/libdvm.so. Error (3): No such process
Stack frame #06 pc 0004d487 /system/lib/libdvm.so (dvmDecodeIndirectRef(Thread*, jobject*)+146): Unable to open symbol file /obj/local/armeabi/libdvm.so. Error (3): No such process
Stack frame #07 pc 0006337d /system/lib/libdvm.so (dvmCallMethodV(Thread*, Method const*, Object*, bool, JValue*, std::_va_list)+156): Unable to open symbol file /obj/local/armeabi/libdvm.so. Error (3):
解决方案
.h文件自己手写的?
JNIEXPORT void JNICALL Java_com_example_jnitest_MainActivity_callBack(
JNIEnv env, jobject obj);
少了 *env