开发app和网站建设那个好些,wordpress适合做网页,网站信息化建设案例,淘宝网官方网站购物商城今天突然接到客服那边的反馈说#xff0c;有玩家反馈进游戏后不久就崩溃了#xff0c;我先是怀疑网络问题#xff0c;因为一连接聊天成功后就挂了。之后用logcat抓日志#xff0c;发现挂在jni那里了 JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8…今天突然接到客服那边的反馈说有玩家反馈进游戏后不久就崩溃了我先是怀疑网络问题因为一连接聊天成功后就挂了。之后用logcat抓日志发现挂在jni那里了 JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xf0 string: in call to NewStringUTF from void org.cocos2dx.lib.Cocos2dxRenderer.nativeRender() 调用JNI的NewStringUTF方法就挂了然后让后台把聊天日志全部拉出来另存为html放到mac机上查看。发现一个特殊的表情如下图所示 我先让后台的同事把所有聊天信息清理干净这时候设备重新登录进去没有问题了。所以确定问题就是这个NewStringUTF方法引起的但部分设备上有问题部分设备没问题。看了一下好像是Android5.0及以后的系统就有此问题问了其它同事发现他们之前遇到过并且处理了。 有二种方案一种是升级NDK另外一种是C传给Java时使用byte[]Java里再把byte[]转成String避免NewStringUTF导致的崩溃。 我用的是cocos2d-x 2.x版本找到CCImage.cpp文件修改getBitmapFromJava方法 bool getBitmapFromJava(const char *text, int nWidth, int nHeight, CCImage::ETextAlign eAlignMask, const char * pFontName, float fontSize){ JniMethodInfo methodInfo; if (! JniHelper::getStaticMethodInfo(methodInfo, org/cocos2dx/lib/Cocos2dxBitmap, createTextBitmap, ([BLjava/lang/String;IIII)V)) { CCLOG(%s %d: error to get methodInfo, __FILE__, __LINE__); return false; } /**create bitmap * this method call Cococs2dx.createBitmap()(java code) to create the bitmap, the java code * will call Java_org_cocos2dx_lib_Cocos2dxBitmap_nativeInitBitmapDC() to init the width, height * and data. * use this appoach to decrease the jni call number */ int strLen strlen(text); jbyteArray byteArray methodInfo.env-NewByteArray(strLen); methodInfo.env-SetByteArrayRegion(byteArray, 0, strLen, reinterpret_castconst jbyte*(text));// jstring jstrText methodInfo.env-NewStringUTF(text); jstring jstrFont methodInfo.env-NewStringUTF(pFontName); methodInfo.env-CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, byteArray, jstrFont, (int)fontSize, eAlignMask, nWidth, nHeight);// methodInfo.env-DeleteLocalRef(jstrText); methodInfo.env-DeleteLocalRef(byteArray); methodInfo.env-DeleteLocalRef(jstrFont); methodInfo.env-DeleteLocalRef(methodInfo.classID); return true;} 注释部分为原来的代码将string替换为byte[]再传给Java即可其它地方如果也遇到JNI崩溃的问题也按上面进行修改即可。 符一个字符串与jbyteArray的互转函数 jbyteArray as_byte_array(unsigned char* buf, int len) { jbyteArray array env-NewByteArray(len); env-SetByteArrayRegion(array, 0, len, reinterpret_castjbyte*(buf)); return array;} unsigned char* as_unsigned_char_array(jbyteArray array) { int len env-GetArrayLength(array); unsigned char* buf new unsigned char[len]; env-GetByteArrayRegion(array, 0, len, reinterpret_castjbyte*(buf)); return buf;} mysql 5.5之前仅支持3个字节如果游戏中有留言等功能要存进数据库的记录那么你就需要过滤这些字符了不然就会插入数据报错。 更多阅读链接 JNI UTF-8 encoding bug with some characters Android ICS 4.0 NDK NewStringUTF is crashing down the App A correct way to convert byte[] in java to unsigned char* in C, and vice versa? emoji处理方式大起底 cocos2d-x android游戏使用自己的字体 Android 上的 制表符tab —— 一个神奇的字符 (cocos2dx crash) Android 上的 制表符tab —— 一个神奇的字符 二 Java Native Interface C and C JNI - University of Cambridge Java Native Interface 探索在Android中使用Emoji Font的方法