android O 32位系统报错:JNI DETECTED ERROR IN APPLICATION: use of deleted local reference

本文主要是介绍android O 32位系统报错:JNI DETECTED ERROR IN APPLICATION: use of deleted local reference,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、同样的程序我在android O 64位系统上跑没有任何问题,但是在32位系统上跑就报这个错误
问题原因是:
自己写的类中有这个构造函数:
public AnWBT_BLE_Service(int service_type, long mostSiguuid, long leastSiguuid, int handle, int end_group_handle, ArrayList<AnWBT_BLE_Characteristic> characteristic) {
    this.service_type = service_type;
    this.mostSiguuid = mostSiguuid;
    this.leastSiguuid = leastSiguuid;
    this.handle = handle;
    this.end_group_handle = end_group_handle;
    this.characteristic = characteristic;
}
然后再JNI调用这个构造函数:
jmethodID service_init = env->GetMethodID(service_cls,"<init>","(IJJIILjava/util/ArrayList;)V");
这种调用方式就会有问题。
二、修改方法:使用没有参数的构造函数,然后使用set方法来设置类里面的成员变量
jclass service_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Service");
jmethodID service_init = env->GetMethodID(service_cls,"<init>","()V");
jmethodID service_setService_type = env->GetMethodID(service_cls,"setService_type","(I)V");
jmethodID service_setMostSiguuid = env->GetMethodID(service_cls,"setMostSiguuid","(J)V");
jmethodID service_setLeastSiguuid = env->GetMethodID(service_cls,"setLeastSiguuid","(J)V");
jmethodID service_setHandle = env->GetMethodID(service_cls,"setHandle","(I)V");
jmethodID service_setEnd_group_handle = env->GetMethodID(service_cls,"setEnd_group_handle","(I)V");
jmethodID service_setCharacteristic = env->GetMethodID(service_cls,"setCharacteristic","(Ljava/util/ArrayList;)V");

三、AnWBT_BLE_Service类的代码

package com.anwsdk.service;import android.os.Parcel;
import android.os.Parcelable;import java.util.ArrayList;
import java.util.List;public class AnWBT_BLE_Service implements Parcelable {private int service_type;private long mostSiguuid;private long leastSiguuid;private int handle;private int end_group_handle;private ArrayList<AnWBT_BLE_Characteristic> characteristic;public AnWBT_BLE_Service() {}public AnWBT_BLE_Service(int service_type, long mostSiguuid, long leastSiguuid, int handle, int end_group_handle, ArrayList<AnWBT_BLE_Characteristic> characteristic) {this.service_type = service_type;this.mostSiguuid = mostSiguuid;this.leastSiguuid = leastSiguuid;this.handle = handle;this.end_group_handle = end_group_handle;this.characteristic = characteristic;}protected AnWBT_BLE_Service(Parcel in) {service_type = in.readInt();mostSiguuid = in.readLong();leastSiguuid = in.readLong();handle = in.readInt();end_group_handle = in.readInt();ArrayList<AnWBT_BLE_Characteristic> characteristic_new = new ArrayList<AnWBT_BLE_Characteristic>();in.readTypedList(characteristic_new,AnWBT_BLE_Characteristic.CREATOR);characteristic = characteristic_new;}public int getService_type() {return service_type;}public long getMostSiguuid() {return mostSiguuid;}public long getLeastSiguuid() {return leastSiguuid;}public int getHandle() {return handle;}public int getEnd_group_handle() {return end_group_handle;}public ArrayList<AnWBT_BLE_Characteristic> getCharacteristic() {return characteristic;}public void setService_type(int service_type) {this.service_type = service_type;}public void setMostSiguuid(long mostSiguuid) {this.mostSiguuid = mostSiguuid;}public void setLeastSiguuid(long leastSiguuid) {this.leastSiguuid = leastSiguuid;}public void setHandle(int handle) {this.handle = handle;}public void setEnd_group_handle(int end_group_handle) {this.end_group_handle = end_group_handle;}public void setCharacteristic(ArrayList<AnWBT_BLE_Characteristic> characteristic) {this.characteristic = characteristic;}@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeInt(service_type);dest.writeLong(mostSiguuid);dest.writeLong(leastSiguuid);dest.writeInt(handle);dest.writeInt(end_group_handle);dest.writeTypedList(characteristic);}public static final Creator<AnWBT_BLE_Service> CREATOR = new Creator<AnWBT_BLE_Service>() {@Overridepublic AnWBT_BLE_Service createFromParcel(Parcel in) {return new AnWBT_BLE_Service(in);}@Overridepublic AnWBT_BLE_Service[] newArray(int size) {return new AnWBT_BLE_Service[size];}};
}

四、JNI层修改前代码

/*
* Class : com_anwsdk_service
* Method :GATT_GetService()
* Signature : (J)Ljava/util/List;
*/
JNIEXPORT jobject JNICALL Java_com_anwsdk_service_anwbtservice_GATT_GetService(JNIEnv *env, jobject,jlong gatt)
{ANW_LOGI("+++ GATT_GetService---");int i = 0;GATT_HANDLE gattHandle = (GATT_HANDLE)gatt;jclass arraylist_cls = env->FindClass("java/util/ArrayList");jmethodID arraylist_init = env->GetMethodID(arraylist_cls,"<init>","()V");	jmethodID arraylist_add = env->GetMethodID(arraylist_cls, "add", "(Ljava/lang/Object;)Z");jobject service_arraylist = env->NewObject(arraylist_cls,arraylist_init,""); if(gattHandle == NULL){return service_arraylist;}AnW_BLE_Device device;GetBLEDeviceManagerByGATTHandle(&device,gattHandle);if(device.gatt_handle == NULL || device.download_status != BLE_DOWNLOAD_STATUS_COMPLETE){return service_arraylist;}jclass descriptor_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Descriptor");jmethodID descriptor_init = env->GetMethodID(descriptor_cls,"<init>","(IJJ[B)V"); jclass characteristic_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Characteristic");jmethodID characteristic_init = env->GetMethodID(characteristic_cls,"<init>","(IIIJJ[BLjava/util/ArrayList;)V");  jclass service_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Service");jmethodID service_init = env->GetMethodID(service_cls,"<init>","(IJJIILjava/util/ArrayList;)V");for(i = 0;i < device.service_count;i++){jobject characteristic_arraylist = env->NewObject(arraylist_cls,arraylist_init,""); AnW_BLE_Service *service = &(device.ble_service[i]);int j = 0;for(j = 0;j < service->characteristic_count;j++){jobject descriptor_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");AnW_BLE_Service_Info_Characteristic *characteristic = &(service->characteristic[j]);jbyteArray characteristic_value = charToByteArray(env, (const char*)characteristic->value,characteristic->value_len);			int k = 0;for(k = 0;k < characteristic->descriptor_count;k++){AnW_BLE_Service_Info_Characteristic_Descriptor *descriptor = &(characteristic->descriptor[k]);jbyteArray descriptor_value = charToByteArray(env, (const char*)descriptor->value,descriptor->value_len);jobject descriptor_obj = env->NewObject(descriptor_cls,descriptor_init,descriptor->handle,*(long *)(descriptor->uuid + 8),*(long *)(descriptor->uuid),descriptor_value); env->CallBooleanMethod(descriptor_arraylist, arraylist_add, descriptor_obj);}jobject characteristic_obj = env->NewObject(characteristic_cls,characteristic_init,characteristic->handle,characteristic->properties,characteristic->value_handle,*(long *)(characteristic->uuid + 8),*(long *)(characteristic->uuid),characteristic_value,descriptor_arraylist); env->CallBooleanMethod(characteristic_arraylist, arraylist_add, characteristic_obj);}jobject service_obj = env->NewObject(service_cls,service_init,service->service_type,*(long *)(service->uuid + 8),*(long *)(service->uuid),service->handle,service->end_group_handle,characteristic_arraylist); env->CallBooleanMethod(service_arraylist, arraylist_add, service_obj);}ANW_LOGI("--- GATT_GetService ---");return service_arraylist;
}

JNI层修改后代码(这里面其实还修改了一个long类型)

jlong uuid_lsb(u_int8* uuid) {jlong lsb = 0;for (int i = 7; i >= 0; i--) {lsb <<= 8;lsb |= uuid[i];}return lsb;
}jlong uuid_msb(u_int8* uuid) {jlong msb = 0;for (int i = 15; i >= 8; i--) {msb <<= 8;msb |= uuid[i];}return msb;
}
/*
* Class : com_anwsdk_service
* Method :GATT_GetService()
* Signature : (J)Ljava/util/List;
*/
JNIEXPORT jobject JNICALL Java_com_anwsdk_service_anwbtservice_GATT_GetService(JNIEnv *env, jobject,jlong gatt){ANW_LOGI("+++ GATT_GetService---(0x%llx)",gatt);int i = 0;GATT_HANDLE gattHandle = (GATT_HANDLE)gatt;jclass arraylist_cls = env->FindClass("java/util/ArrayList");jmethodID arraylist_init = env->GetMethodID(arraylist_cls,"<init>","()V");	jmethodID arraylist_add = env->GetMethodID(arraylist_cls, "add", "(Ljava/lang/Object;)Z");jobject service_arraylist = env->NewObject(arraylist_cls,arraylist_init,""); if(gattHandle == NULL){return service_arraylist;}AnW_BLE_Device device;GetBLEDeviceManagerByGATTHandle(&device,gattHandle);if(device.gatt_handle == NULL || device.download_status != BLE_DOWNLOAD_STATUS_COMPLETE){return service_arraylist;}jclass descriptor_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Descriptor");jmethodID descriptor_init = env->GetMethodID(descriptor_cls,"<init>","()V"); jmethodID descriptor_setHandle = env->GetMethodID(descriptor_cls,"setHandle","(I)V");jmethodID descriptor_setMostSiguuid = env->GetMethodID(descriptor_cls,"setMostSiguuid","(J)V");jmethodID descriptor_setLeastSiguuid = env->GetMethodID(descriptor_cls,"setLeastSiguuid","(J)V");jmethodID descriptor_setValue = env->GetMethodID(descriptor_cls,"setValue","([B)V");jclass characteristic_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Characteristic");jmethodID characteristic_init = env->GetMethodID(characteristic_cls,"<init>","()V");jmethodID characteristic_setHandle = env->GetMethodID(characteristic_cls,"setHandle","(I)V");jmethodID characteristic_setProperties = env->GetMethodID(characteristic_cls,"setProperties","(I)V");jmethodID characteristic_setValue_handle = env->GetMethodID(characteristic_cls,"setValue_handle","(I)V");jmethodID characteristic_setMostSiguuid = env->GetMethodID(characteristic_cls,"setMostSiguuid","(J)V");jmethodID characteristic_setLeastSiguuid = env->GetMethodID(characteristic_cls,"setLeastSiguuid","(J)V");jmethodID characteristic_setValue = env->GetMethodID(characteristic_cls,"setValue","([B)V");jmethodID characteristic_setDescriptor = env->GetMethodID(characteristic_cls,"setDescriptor","(Ljava/util/ArrayList;)V");jclass service_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Service");jmethodID service_init = env->GetMethodID(service_cls,"<init>","()V");jmethodID service_setService_type = env->GetMethodID(service_cls,"setService_type","(I)V");jmethodID service_setMostSiguuid = env->GetMethodID(service_cls,"setMostSiguuid","(J)V");jmethodID service_setLeastSiguuid = env->GetMethodID(service_cls,"setLeastSiguuid","(J)V");jmethodID service_setHandle = env->GetMethodID(service_cls,"setHandle","(I)V");jmethodID service_setEnd_group_handle = env->GetMethodID(service_cls,"setEnd_group_handle","(I)V");jmethodID service_setCharacteristic = env->GetMethodID(service_cls,"setCharacteristic","(Ljava/util/ArrayList;)V");for(i = 0;i < device.service_count;i++){jobject characteristic_arraylist = env->NewObject(arraylist_cls,arraylist_init,""); AnW_BLE_Service *service = device.ble_service + i;int j = 0;for(j = 0;j < service->characteristic_count;j++){jobject descriptor_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");AnW_BLE_Service_Info_Characteristic *characteristic = &(service->characteristic[j]);jbyteArray characteristic_value = charToByteArray(env, (const char*)characteristic->value,characteristic->value_len);			int k = 0;for(k = 0;k < characteristic->descriptor_count;k++){AnW_BLE_Service_Info_Characteristic_Descriptor *descriptor = &(characteristic->descriptor[k]);jbyteArray descriptor_value = charToByteArray(env, (const char*)descriptor->value,descriptor->value_len);jobject descriptor_obj = env->NewObject(descriptor_cls,descriptor_init,""); env->CallVoidMethod(descriptor_obj,descriptor_setHandle,descriptor->handle);env->CallVoidMethod(descriptor_obj,descriptor_setMostSiguuid,uuid_msb(descriptor->uuid));env->CallVoidMethod(descriptor_obj,descriptor_setLeastSiguuid,uuid_lsb(descriptor->uuid));env->CallVoidMethod(descriptor_obj,descriptor_setValue,NULL);env->CallBooleanMethod(descriptor_arraylist, arraylist_add, descriptor_obj);}jobject characteristic_obj = env->NewObject(characteristic_cls,characteristic_init,""); env->CallVoidMethod(characteristic_obj,characteristic_setHandle,characteristic->handle);env->CallVoidMethod(characteristic_obj,characteristic_setProperties,characteristic->properties);env->CallVoidMethod(characteristic_obj,characteristic_setValue_handle,characteristic->value_handle);env->CallVoidMethod(characteristic_obj,characteristic_setMostSiguuid,uuid_msb(characteristic->uuid));env->CallVoidMethod(characteristic_obj,characteristic_setLeastSiguuid,uuid_lsb(characteristic->uuid));env->CallVoidMethod(characteristic_obj,characteristic_setValue,NULL);env->CallVoidMethod(characteristic_obj,characteristic_setDescriptor,descriptor_arraylist);env->CallBooleanMethod(characteristic_arraylist, arraylist_add, characteristic_obj);}jobject service_obj = env->NewObject(service_cls,service_init,"");env->CallVoidMethod(service_obj,service_setService_type,service->service_type);env->CallVoidMethod(service_obj,service_setMostSiguuid,uuid_msb(service->uuid));env->CallVoidMethod(service_obj,service_setLeastSiguuid,uuid_lsb(service->uuid));env->CallVoidMethod(service_obj,service_setHandle,service->handle);env->CallVoidMethod(service_obj,service_setEnd_group_handle,service->end_group_handle);env->CallVoidMethod(service_obj,service_setCharacteristic,characteristic_arraylist);env->CallBooleanMethod(service_arraylist, arraylist_add, service_obj);}ANW_LOGI("--- GATT_GetService ---");return service_arraylist;
}

这篇关于android O 32位系统报错:JNI DETECTED ERROR IN APPLICATION: use of deleted local reference的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/699884

相关文章

什么是cron? Linux系统下Cron定时任务使用指南

《什么是cron?Linux系统下Cron定时任务使用指南》在日常的Linux系统管理和维护中,定时执行任务是非常常见的需求,你可能需要每天执行备份任务、清理系统日志或运行特定的脚本,而不想每天... 在管理 linux 服务器的过程中,总有一些任务需要我们定期或重复执行。就比如备份任务,通常会选在服务器资

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

TP-LINK/水星和hasivo交换机怎么选? 三款网管交换机系统功能对比

《TP-LINK/水星和hasivo交换机怎么选?三款网管交换机系统功能对比》今天选了三款都是”8+1″的2.5G网管交换机,分别是TP-LINK水星和hasivo交换机,该怎么选呢?这些交换机功... TP-LINK、水星和hasivo这三台交换机都是”8+1″的2.5G网管交换机,我手里的China编程has

使用Vue.js报错:ReferenceError: “Vue is not defined“ 的原因与解决方案

《使用Vue.js报错:ReferenceError:“Vueisnotdefined“的原因与解决方案》在前端开发中,ReferenceError:Vueisnotdefined是一个常见... 目录一、错误描述二、错误成因分析三、解决方案1. 检查 vue.js 的引入方式2. 验证 npm 安装3.

PHP执行php.exe -v命令报错的解决方案

《PHP执行php.exe-v命令报错的解决方案》:本文主要介绍PHP执行php.exe-v命令报错的解决方案,文中通过图文讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下... 目录执行phpandroid.exe -v命令报错解决方案执行php.exe -v命令报错-PHP War

Android WebView的加载超时处理方案

《AndroidWebView的加载超时处理方案》在Android开发中,WebView是一个常用的组件,用于在应用中嵌入网页,然而,当网络状况不佳或页面加载过慢时,用户可能会遇到加载超时的问题,本... 目录引言一、WebView加载超时的原因二、加载超时处理方案1. 使用Handler和Timer进行超

基于Qt实现系统主题感知功能

《基于Qt实现系统主题感知功能》在现代桌面应用程序开发中,系统主题感知是一项重要的功能,它使得应用程序能够根据用户的系统主题设置(如深色模式或浅色模式)自动调整其外观,Qt作为一个跨平台的C++图形用... 目录【正文开始】一、使用效果二、系统主题感知助手类(SystemThemeHelper)三、实现细节

CentOS系统使用yum命令报错问题及解决

《CentOS系统使用yum命令报错问题及解决》文章主要讲述了在CentOS系统中使用yum命令时遇到的错误,并提供了个人解决方法,希望对大家有所帮助,并鼓励大家支持脚本之家... 目录Centos系统使用yum命令报错找到文件替换源文件为总结CentOS系统使用yum命令报错http://www.cppc

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

基于人工智能的图像分类系统

目录 引言项目背景环境准备 硬件要求软件安装与配置系统设计 系统架构关键技术代码示例 数据预处理模型训练模型预测应用场景结论 1. 引言 图像分类是计算机视觉中的一个重要任务,目标是自动识别图像中的对象类别。通过卷积神经网络(CNN)等深度学习技术,我们可以构建高效的图像分类系统,广泛应用于自动驾驶、医疗影像诊断、监控分析等领域。本文将介绍如何构建一个基于人工智能的图像分类系统,包括环境