在我的游戏中,我使用来开发Android和IOS版本。现在我需要展示视频(仅限于Android)。我打算将其显示在对话框中(位于游戏视图之上)。问题是,我没有任何活动可用于显示对话框(因为对话框只能在活动中显示)。尽管在cocos2dx库文件夹中有一个,但我不知道如何使用它。从C++代码中,我调用了Java类的静态方法,如下所示:
我尝试像这篇文章中所述那样使用
那么,我的方法正确吗?如果不正确,请建议一种实现相同功能的方法。谢谢。 编辑: 最终找到了答案。早些时候,我尝试在应用程序静态上下文中在UI线程上运行,就像这个链接中所述,但是没有成功。当我使用Cocos2dxActivity活动实例时,我得到了结果。
void LMJNICommunicator::showVideo()
{
LOGD("initialiseDatabase inside LMJNICommunicator");
jmethodID methodID = 0;
JNIEnv *pEnv = 0;
pEnv = getJNIEnv();
jclass ret = pEnv->FindClass("com/mobinius/lostmonstersclass/LMDatabaseDataManager");
methodID = pEnv->GetStaticMethodID(ret, "showVideo", "()V");
if (! methodID)
{
LOGD("Failed to find static method id of %s", "showVideo");
return;
}
pEnv->CallStaticVoidMethod(ret,methodID);
pEnv->DeleteLocalRef(ret);
}
我从C++代码中调用的静态方法(在普通Java类中)
Class LMDatabaseDataManager {
public static void showVideo() {
Dialog dialog = new Dialog(Cocos2dxActivity.getInstance());
dialog.show();
// getting Can't create handler inside thread that has not called Looper.prepare() error
}
}
我尝试像这篇文章中所述那样使用
Handler
,但没有得到结果(获得了该文章中相同的错误)。我还尝试获取静态的Context
,就像这篇文章中所述一样。那么,我的方法正确吗?如果不正确,请建议一种实现相同功能的方法。谢谢。 编辑: 最终找到了答案。早些时候,我尝试在应用程序静态上下文中在UI线程上运行,就像这个链接中所述,但是没有成功。当我使用Cocos2dxActivity活动实例时,我得到了结果。
Class LMDatabaseDataManager {
public static void showVideo() {
Cocos2dxActivity.getInstance().runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Dialog dialog = new Dialog(Cocos2dxActivity.getInstance());
dialog.show();
}
});
}
}