| Summary: | SDL_AndroidGetActivity() does not work. | ||
|---|---|---|---|
| Product: | SDL | Reporter: | Pallav Nawani <pallavnawani> |
| Component: | main | Assignee: | Gabriel Jacobo <gabomdq> |
| Status: | RESOLVED FIXED | QA Contact: | Sam Lantinga <slouken> |
| Severity: | major | ||
| Priority: | P2 | CC: | gabomdq, philipp.wiesemann |
| Version: | HG 2.0 | ||
| Hardware: | All | ||
| OS: | Android (All) | ||
Good find! Fixed in http://hg.libsdl.org/SDL/rev/0cb47cc139d3 You should remember to delete the local reference manually! (In reply to comment #2) > Fixed in http://hg.libsdl.org/SDL/rev/0cb47cc139d3 > > You should remember to delete the local reference manually! Maybe it would be useful to add this warning in the SDL_system.h file because SDL_AndroidGetActivity() is meant for library users and most of them will not look into the implementation itself (the function is not used internally). |
The function returns a local jobject, which goes out of scope as soon as function returns, thanks to LocalReferenceHolder refs(__FUNCTION__); This code works: AAssetManager *IcOSAndroidGetAssetManager() { SDL_Log("%s(): Attempting to obtain JNI pointer", __FUNCTION__); JNIEnv *aEnv = (JNIEnv *)SDL_AndroidGetJNIEnv(); assert(aEnv); SDL_Log("%s(): Got JNI", __FUNCTION__); jclass aSdlActivityClass = aEnv->FindClass("org/libsdl/app/SDLActivity"); SDL_Log("%s(): Got SDL Class (%x)", __FUNCTION__, aSdlActivityClass); jmethodID aStaticMid = aEnv->GetStaticMethodID(aSdlActivityClass, "getContext", "()Landroid/content/Context;"); jobject aContext = aEnv->CallStaticObjectMethod(aSdlActivityClass, aStaticMid); //jobject aContext = (jobject)SDL_AndroidGetActivity(); SDL_Log("%s(): Got Activity (%x)", __FUNCTION__, aContext); jclass aSdlClass = aEnv->FindClass("android/content/Context"); SDL_Log("%s(): Got Class (%x)", __FUNCTION__, aSdlClass); jmethodID aJavaMethodID = aEnv->GetMethodID(aSdlClass, "getAssets", "()Landroid/content/res/AssetManager;"); SDL_Log("%s(): Got Method ID (%x)", __FUNCTION__, aJavaMethodID); jobject aJavaAssetManager = aEnv->CallObjectMethod(aContext, aJavaMethodID); SDL_Log("%s(): Got Asset Manager (%x)", __FUNCTION__, aJavaAssetManager); return AAssetManager_fromJava(aEnv, aJavaAssetManager); } Uncomment the commented line to make it fail.