Add View to Opengl View

Add View to Opengl View
0.0 0


Hello guys

I have ported my iphone app to android using ndk and cocos2dx.
This has worked like a charm and I think cocos2dx is very cool!

Now I would like to add some Java views to my main opengl view in the Java environment.
And this is not really working for me. I think I need basic knowledge about have views, activitis, Intent etc works in the Java environment.

To be specific I need to add a TextView (java) to my opengl view at runtime.
I have tried the following but it crashes when I call the function public void testSetText() .

public class myTest extends Cocos2dxActivity{
    private static final String TAG = "MY_TEST";

    private FrameLayout mainFrame; 
    protected void onCreate(Bundle savedInstanceState){

        Log.e(TAG, "onCreate");

        String packageName = getApplication().getPackageName();

        mainFrame = new FrameLayout(this);

        mGLView = new Cocos2dxGLSurfaceView(this);


        RelativeLayout base = new RelativeLayout(this);


    private GLSurfaceView mGLView;

    static {

     protected void onPause() {      
         Log.i("TAG"," onPause");        


     protected void onResume() {         
         Log.i("TAG"," onResume");       

    protected void onStart() {
        // TODO Auto-generated method stub              
        Log.e(TAG, "onStart");


    protected void onStop() {
        // TODO Auto-generated method stub      
        Log.e(TAG, "onStop");

    public void testSetText(){
        Log.e(TAG, "testSetText");  

        TextView textView = new TextView(this);      
        textView.setText("Hello, Android");

        LinearLayout testLayout = new LinearLayout(this);

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);       
        testLayout.addView(textView ,lp);


And if I instead try to run another activity- or call setContentView(R.layout.test_screen) in the function public void testSetText() my game also crashes.

Could anyone give me some good advise here?
Thank you so much.


Okay I am still working on this issue and just cant get it fixed :frowning:
But I have found out something interesting:

So to sum up my problem:
My android app has a cocos2dx scene running which works perfect. Then when the user push a button :

myBut = CCMenuItemImage::itemFromNormalImage("some.png", "some.png.png", this, menu_selector(Cocos2dMenuScene::butPushed));

I call the Java environment through JNI, and request to add some kind of view ( eg a TextView ) ( see testSetText() ). At this point my app crashes - with crashlog:

WARN/dalvikvm(8352): threadid=11: thread exiting with uncaught exception (group=0x40015560) 08-16 15:19:52.214: ERROR/AndroidRuntime(8352): FATAL EXCEPTION: GLThread 10 08-16 15:19:52.214: ERROR/AndroidRuntime(8352):

Now I also use Admob which is integrated at the Java end. When Admob call my main class( myTest see above ) through some delegate (eg onReceiveAd ) - I have tried to call testSetText() and add my TextView at this point- and everything works fine!

So I think it has someting to do with threading - But I am not a specialist in this area so I could very much need a little help.
Any suggestions?


Cocos2d-x running in the rendering thread, which is different from UI thread. The widget should be operated in UI thread. so you can not add TextView from jni, which is running in rendering thread. You can refer Cocos2dxActivity.showDialog(). It use Handler to deal with it.


@esben hansen Would you mind to share your code that integrates admob with cocos2d-x?