Cocos2dx C++中调用java非静态函数。
CPP中调用JAVA中的非静态方法需要先获得Activity的对象,这是区别所在。
在AppActivity中声明一个静态变量
public static AppActivity con;
在AppActivity的create中对此变量进行赋值
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
con = this;
}
在AppActivity中添加一个获得该对象的方法
public static Object returnActivity() {
return con;
}
在cpp中添加方法
void HelloWorld::onClickLogin()
{
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
{
JniMethodInfo info;
jobject activityObj;
bool isHaveObj = JniHelper::getStaticMethodInfo(info,"org/cocos2dx/cpp/AppActivity","returnActivity","()Ljava/lang/Object;");
if (isHaveObj)
{
activityObj = info.env->CallStaticObjectMethod(info.classID,info.methodID);
log("jni:obj has find.");
}
else
{
log("jni:obj is null");
}
JniMethodInfo methodInfo;
bool isHaveMethod = JniHelper::getMethodInfo(methodInfo,"onClickLogin","()V");
if (isHaveMethod)
{
methodInfo.env->CallVoidMethod(activityObj,methodInfo.methodID);
}
}
#endif
}
原文链接:https://www.f2er.com/cocos2dx/338198.html