如何从一个静态方法显示对话框 [英] How to show Dialog from a static method

查看:337
本文介绍了如何从一个静态方法显示对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的比赛,这是为Android和iOS使用 cocos2dx 做的,我必须展示视频(Android版)。我打算以显示它的对话框(在游戏视图顶部)。问题是,我没有引用显示对话框(如对话框只能在活动中显示)的任何活动。即便如此,在cocos2dx lib文件夹中,有一个 Cocos2dxActivity ,但我没有得到如何利用它。从C ++ code,我打电话从Java类中的静态方法如下

 无效LMJNICommunicator :: showVideo()
{
     LOGD(initialiseDatabase内LMJNICommunicator);     jmethodID放在methodID = 0;
     JNIEnv的* pEnv = 0;
     pEnv = getJNIEnv();
     JCLASS RET = pEnv->的findClass(COM / mobinius / lostmonstersclass / LMDatabaseDataManager);
     放在methodID = pEnv-> GetStaticMethodID(保留showVideo,()V);     如果(!放在methodID)
     {
          LOGD(无法找到%S的静态方法ID,showVideo);
          返回;
     }     pEnv-> CallStaticVoidMethod(RET,放在methodID);
     pEnv-> DeleteLocalRef(RET);}

静态方法(这是正常的Java类),这我是从C ++ code

调用

 类LMDatabaseDataManager {    公共静态无效showVideo(){         对话的对话=新的对话框(Cocos2dxActivity.getInstance());
         dialog.show();
        //获得内螺纹无法创建处理程序,也没有所谓的活套。prepare()错误
    }
}

我试图使用处理程序的像<一个href=\"http://stackoverflow.com/questions/11483275/how-can-i-send-message-to-handler-from-a-static-function\">this但没有得到结果(在该职位得到了同样的错误)。
也试图获取静态上下文喜欢的这个

那么,是我的方法正确吗?如果不正确,请提出一个方法我怎么能实现相同。谢谢你。

编辑:

最后得到的回答这一点。早些时候,我试着与应用静态上下文UI线程运行,如链接但没有得到......与Cocos2dxActivity活动实例我知道了。

 类LMDatabaseDataManager {    公共静态无效showVideo(){    Cocos2dxActivity.getInstance()。runOnUiThread(新的Runnable(){        @覆盖
        公共无效的run(){
            // TODO自动生成方法存根
            对话的对话=新的对话框(Cocos2dxActivity.getInstance());
            dialog.show();
        }
    });    }
}


解决方案

尝试在Cocos2dxActivity加入适当的行:

 公共类Cocos2dxActivity延伸活动{
    私有静态Cocos2dxActivity例如= NULL;
   @覆盖公共无效的onCreate(包B){
     ...
     this.instance =这一点;
     }     公共静态Cocos2dxActivity的getInstance(){
        返回实例;
     }}

当您要创建您的对话框:

 如果(Cocos2dxActivity.getInstance()!= NULL){
    AlertDialog对话框=新AlertDialog(Cocos2dxActivity.getInstance());
    //你的对话框code的其余部分放在这里
}

In my game, which is done for both Android and IOS using cocos2dx, I have to show video(for Android). I am planning to show it in Dialog(on top of game view). Problem is that, I don't have any Activity referenced to show Dialog(as Dialogs can only be shown in Activities). Even though, In cocos2dx lib folder, there is a Cocos2dxActivity but I am not getting how to make use of it. From C++ code, I am calling a static method from Java class as below

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);

}

Static method(which is in normal Java class) which I am calling from C++ code

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
    }
}

I tried to make use of Handler like this but did not get result(got same error in that post). Also tried to get a static Context like this.

So, is my way correct? If not correct, please suggest a way how can I implement the same. Thanks.

Edit:

Finally got answer for this. Earlier I tried running on UI thread with Application static context as in this link but did not get... with Cocos2dxActivity activity instance I got it.

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();            
        }
    });

    }
}

解决方案

Try adding the appropriate lines in Cocos2dxActivity:

public class Cocos2dxActivity extends Activity {
    private static Cocos2dxActivity instance = null;
   @Override public void onCreate(Bundle b) {
     ...
     this.instance = this;
     }

     public static Cocos2dxActivity getInstance() {
        return instance;
     }



}

When you want to create your dialog:

if (Cocos2dxActivity.getInstance() != null)  {
    AlertDialog dialog = new AlertDialog(Cocos2dxActivity.getInstance());
    // rest of your dialog code goes here
}

这篇关于如何从一个静态方法显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆