java-安卓 JNI回调Java 总是崩溃

问题描述

安卓 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

时间: 2024-09-16 12:00:50

java-安卓 JNI回调Java 总是崩溃的相关文章

anroid jni回调java函数问题

问题描述 anroid jni回调java函数问题 JAVA 代码 1.NativeProviefder.java package com.example.test; public class NativeProviefder { static { System.loadLibrary("test_jni"); } public native void nativeInitilize(); public native void start(); public native void s

jni调用java方法报错!!!

问题描述 jni调用java方法报错!!! 12-04 11:33:08.818: A/art(23825): art/runtime/check_jni.cc:70] JNI DETECTED ERROR IN APPLICATION: calling static method java.lang.String com.library.utils.MD5Utils.md5(java.lang.String) with CallObjectMethod 12-04 11:33:08.818:

Java通过JNI调用libcurl导致JVM崩溃

问题描述 通过Java的JNI调用libcurl导致JVM崩溃,不知道什么原因大家帮看看,并不是刚上来就崩溃,而是执行一段就崩溃,JVM崩溃的log如下:##AfatalerrorhasbeendetectedbytheJavaRuntimeEnvironment:##SIGSEGV(0xb)atpc=0x0000003163221ca2,pid=12648,tid=47588522796480##JREversion:6.0_23-b05#JavaVM:JavaHotSpot(TM)64-Bi

解析Java的JNI编程中的对象引用与内存泄漏问题_java

JNI,Java Native Interface,是 native code 的编程接口.JNI 使 Java 代码程序可以与 native code 交互--在 Java 程序中调用 native code:在 native code 中嵌入 Java 虚拟机调用 Java 的代码. JNI 编程在软件开发中运用广泛,其优势可以归结为以下几点: 利用 native code 的平台相关性,在平台相关的编程中彰显优势. 对 native code 的代码重用. native code 底层操作

Java通过JNI调用C语言的方法

JAVA以其跨平台的特性深受人们喜爱,而又正由于它的跨平台的目的,使得它和本地机器的各种内部联系变得很少,约束了它的功能. 解决JAVA对本地操作的一种方法就是JNI. JAVA通过JNI调用本地方法,而本地方法是以库文件的形式存放的(在WINDOWS平台上是DLL文件形式,在UNIX机器上是SO文件形式).通过调用本地的库文件的内部方法,使JAVA可以实现和本地机器的紧密联系,调用系统级的各接口方法. 简单介绍及应用如下: 一.JAVA中所需要做的工作 在JAVA程序中,首先需要在类中声明所调

Java通过JNI调用C++程序

JNI是Java Native Interface的缩写,中文为JAVA本地调用.使用JNI可以很方便的用我们的Java程序调用C/C++程序.很多时候,某些功能用Java无法 实现,比如说涉及到底层驱动的一些功能,这时候我们就可以利用JNI来调用C或者C++程序来实现,这就是JNI的强大之处.但是JNI也有它的缺点,使 用java与本地已编译的代码交互,通常会丧失平台可移植性. 下面是一个JNI例子,调用C++输出"hello world":   第一步:创建Java类,在里面定义一

this 以及接口问题java 安卓

问题描述 this 以及接口问题java 安卓 myButton.addActionListener(this);? myFrame.addMouseMotionListener(this);? myFrame.addMouseListener(this);? myFrame.addWindowListener(this);? myButton.addActionListener(this);? myFrame.addMouseMotionListener(this);? myFrame.add

android-eclicpe怎么同时导入多个安卓项目和java项目呢

问题描述 eclicpe怎么同时导入多个安卓项目和java项目呢 如题,我从其他地方复制了二十多个安卓项目和java项目,但是一个一个导入太麻烦了,有哪位大神知道怎么一次性的导入安卓项目或者java项目吗?求大神赐教!不吝感激 解决方案 把你的项目放到一个文件夹中,然后导入时选择你需要的项目应该就可以了 解决方案二: 但是一般不建议这样.一起倒入会很卡. 解决方案三: File-->New-->Project-->New Project-->Android Project from

vm-eclipse安装了安卓模拟器后,java虚拟机就连不上,不能debug

问题描述 eclipse安装了安卓模拟器后,java虚拟机就连不上,不能debug eclipse版本为juno,debug时出现下面错误 Cannot connect to VM Socket operation on nonsocket: configureBlocking 上面错误是用在安装了安卓的模拟器后,可以debug安卓的程序,但是写一个带mian方法的java类,debug后就报这个错误.修改配置,安装jdk都已经试过了,听一个安卓开发的同事说,安了安卓模拟器后,是连不上java虚