Search code examples
javaandroidparameterscrashjava-native-interface

JNI Function Causing Android App to Crash


I have the following JNI function that sends an event to my Java code.

void onIncrementAchievement(unsigned char _iArgumentCount, const void *_pArguments, void *_pUserData){
JNIEnv *pJNIEnv = GetJNIEnv();
if (pJNIEnv){

        if ( _pArguments && ( _iArgumentCount > 0 ) ){
                const S3DX::AIVariable *pVariables = (const S3DX::AIVariable *)_pArguments ;

                if(_iArgumentCount != 2)
                        LOGI("INCORRECT NUMBER OF PARAMETERS");
                else{
                        if(pVariables[0].GetType() == S3DX::AIVariable::eTypeString){
                                // CHANGE ME!
                                jclass pJNIActivityClass = pJNIEnv->FindClass ( "com/nurfacegames/testgame01/TestGame01" );

                                if(pJNIActivityClass == NULL)
                                        LOGI("jclass was null!?!");
                                else{
                                        jmethodID pJNIMethodID = pJNIEnv->GetStaticMethodID(pJNIActivityClass, "onIncrementAchievement", "(Ljava/lang/String;Ljava/lang/Integer;)V");

                                        if(pJNIMethodID == NULL)
                                                LOGI("jmethodID was null!?!?");
                                        else{
                                                //Create a new string
                                                jstring arg;
                                                arg = pJNIEnv->NewStringUTF(pVariables[0].GetStringValue());

                                                jint arg2 = pVariables[1].GetNumberValue();

                                                //Call the method and pass the string parameter along
                                                pJNIEnv->CallStaticVoidMethod(pJNIActivityClass, pJNIMethodID, arg, arg2);
                                                //Free the string
                                                pJNIEnv->DeleteLocalRef(arg);
                                        }
                                }
                        }
                }
        }
}
}

When I run my Android App, it force closes without even giving a single error in logcat, and when I compile with Ant, there are no compile errors.

I hate to post such a vague question, but if someone has an idea what is wrong with my JNI code, please give me a hint. Thanks!

The areas that I think have a problem (that I have been working on) are:

 if(_iArgumentCount != 2)
                        LOGI("INCORRECT NUMBER OF PARAMETERS");
                else{

and this section:

jint arg2 = pVariables[1].GetNumberValue();

pJNIEnv->CallStaticVoidMethod(pJNIActivityClass, pJNIMethodID, arg, arg2);

Thanks!


Solution

  • Well, look at your method name void onIncrementAchievement, as far as I know, that isn't the right way to declare your method in native section, it should be:

    Java_com_example_yourpackagename_youractivityname_yourmethod(...)
    

    And one more thing, the arguments in your method are also wrong I guess, check out this tutorial.