2

I am getting the following error while trying to call a function in java from cpp class to save a boolean in memmory. I am using a class called MyAdapter.cpp to call function to MyAdapterJni.cpp. I have following function written in MyAdapterJni.cpp

bool GetBooleanJni(const char *key, bool defaultValue)
{
     cocos2d::JniMethodInfo methodInfo;
    jboolean ret = false;
    if (! getStaticMethodInfo(methodInfo, "GetBoolean", "(Ljava/lang/String;Z)Z"))
    {
        return ret;
    }
    ret = methodInfo.env->CallStaticBooleanMethod(methodInfo.classID, methodInfo.methodID, defaultValue);
    methodInfo.env->DeleteLocalRef(methodInfo.classID);

    return ret;
}

and i have following function in myManager.java class

public static boolean GetBoolean(String key, boolean defaultValue)
{
    return sharedPreferences.getBoolean(key, defaultValue);
}

i get the following log on my logcat

12-14 12:06:32.024: W/dalvikvm(9575): Exception Ljava/lang/NullPointerException; thrown while initializing Lcom/mygames/Game/MyManager;

12-14 12:06:32.024: D/libMyManager(9575): Failed to find static method id of GetBoolean

My java class is inside package com.mygames.Game

Can anyone please tell me what can possibly cause this error

4

3 回答 3

1

The initialization of MyManager throws a NullPointerException, which probably prevents determining the method id, so you might want to look at that. Note that not only the class initializer could throw this but also initialization of any static fields. You could put all initialization into the class initializer and then debug from there, something there must be null.

于 2012-12-14T08:00:31.643 回答
0

Try to get the instance of the activity in which you running this (say cocos2dxActivity) and do the implementation in runonUiThread....

public static boolean GetBoolean(String key, boolean defaultValue) {
Cocos2dxActivity.getInstance().runOnUiThread(new Runnable() {

        @Override
        public void run() {
                 return sharedPreferences.getBoolean(key, defaultValue);   
        }
    });

    }
}

Note: It might give you some error of making function parameters. Hope you understood if not please feel free to ask ...

于 2012-12-17T17:15:37.313 回答
0

i was also having this issue, and in my case, running my java method functionality on main UI thread solved my problem. e.g. inside your java method

Activity ac=(Activity) context;
     ac.runOnUiThread(new Runnable() 
     {
         public void run() 
            {
                 //your code here
            }
     });
于 2014-12-18T12:18:34.693 回答