android - getObjectClass中的android jni崩溃
问题描述
崩溃很少发生在某些 android 设备上。
该代码调用java类中的一个方法,该方法返回一个字符串值,该值被添加到现有字符串并返回给调用者。
我错过了什么?
这是回溯:
#00 pc 000000000004a3d8 /system/lib/libc.so (tgkill+12)
#01 pc 0000000000047b53 /system/lib/libc.so (pthread_kill+34)
#02 pc 000000000001d6a9 /system/lib/libc.so (raise+10)
#03 pc 00000000000191f5 /system/lib/libc.so (__libc_android_abort+34)
#04 pc 0000000000017264 /system/lib/libc.so (abort+4)
#05 pc 000000000031b859 /system/lib/libart.so (_ZN3art7Runtime5AbortEPKc+328)
#06 pc 00000000000b5305 /system/lib/libart.so (_ZN3art10LogMessageD2Ev+1132)
#07 pc 0000000000239d35 /system/lib/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+1664)
#08 pc 0000000000239f9b /system/lib/libart.so (_ZN3art9JavaVMExt9JniAbortFEPKcS2_z+66)
#09 pc 0000000000264fb7 /system/lib/libart.so (_ZN3art3JNI14GetObjectClassEP7_JNIEnvP8_jobject+478)
#10 pc 00000000000852cd /data/app/xxx.xxx.xxx-1/lib/arm/libmain.so (_ZN8JNIcalls3GetENSt6__ndk112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE+52)
#11 pc 00000000000854d5 /data/app/xxx.xxx.xxx-1/lib/arm/libmain.so (_ZN8JNIcalls6UpdateEd+124)
这是 jni 代码:
std::string JNIcalls::Get(std::string key)
{
std::string value = "";
#ifdef __ANDROID__
// retrieve the JNI environment.
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
// retrieve the Java instance of the SDLActivity
jobject activity = (jobject)SDL_AndroidGetActivity();
// find the Java class of the activity. It should be SDLActivity or a subclass of it.
jclass clazz(env->GetObjectClass(activity));
// find the identifier of the method to call
jmethodID method_id = env->GetStaticMethodID(clazz, "ImpJNIcallsGet", "(Ljava/lang/String;)Ljava/lang/String;");
jstring jkey = env->NewStringUTF(key.c_str());
// effectively call the Java method
jstring rv = (jstring)env->CallStaticObjectMethod(clazz, method_id, jkey);
if(rv != NULL) {
const char *strReturn = env->GetStringUTFChars(rv, 0);
int strlen_size = strlen(strReturn);
for(int i = 0; i < strlen_size; i++)
{
value.append(1,strReturn[i]);
}
env->DeleteLocalRef(rv);
}
// clean up the local references.
env->DeleteLocalRef(activity);
env->DeleteLocalRef(clazz);
env->DeleteLocalRef(jkey);
#endif
return value;
}
void JNIcalls::Update()
{
std::string result = JNIcalls::Get("reward_video_watched");
//more code
}
解决方案
推荐阅读
- android - android 可以签署 http/https 调用,以唯一标识执行请求的应用程序吗?
- c# - 设置 RDOSession.MAPIObject 时出现“不支持接口”ComException
- python - 更新值时 SQLAlchemy 无法设置属性错误
- fragment - sap.ui.core.Fragment.load 返回未定义
- c# - 如何使用 LINQ 在我的数据库中的调查中找到带有答案的问题数量
- python - 单击表单上的提交后未重定向到 Home.html 页面
- ruby-on-rails - 活动管理员嵌套资源表单生成
- ruby-on-rails - 如何进行安全的 Rails PayPal 支付?
- javascript - 如何使非 Firebase 用户无法访问页面?
- codeigniter - 如何使用 mvc 结构使用 codeigniter 查看更新?