Search code examples
androidjava-native-interfacecocos2d-xruntimeexception

How to display custom dialog in Cocos2dxActivity


How can I display a custom android dialog(with custom layout) in Cocos2dxActivity ? I tried calling a method through JNI in which I created a Dialog, set its layout and displayed it.

public void displayDialog()
 {
    Dialog d=new Dialog(this);
    d.setContectView(R.layout.myDialog);
    d.show();
 }

It gives me this error

08-21 14:34:08.045: E/AndroidRuntime(2675): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()


Solution

  • me is an Activity, in your case me = this; the key point is to use runOnUiThread(new Runnable(){public void run(){}}

    public void rateMe(String s){
        me.runOnUiThread(new Runnable() {
              public void run() {
                AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
                dialog.setTitle("Rate Me");
    
                dialog.setMessage("If you enjoy this game, please take a moment to rate it. Thanks for your support!");
                //tv.setWidth(240);
                dialog.setPositiveButton("Rate Now",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0,
                                    int arg1) {
                                mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.home.test")));
                            }
    
                        });
    
                dialog.setNegativeButton("No, thanks",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0,
                                    int arg1) {
    
                            }
                        });       
                dialog.show();    
              }
        });
    }