简体   繁体   中英

How to put GLSurfaceView inside a layout on android

I would like to create an app where the glsurfaceview is inside a relative layout together with a listview. While coding, my IDE shows no errors, but once I run the application it just crashes.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <ListView
        android:layout_height="wrap_content"
        android:layout_width="100dp"
        android:background="#6D6D6D"
        android:id="@+id/listObjects"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_centerHorizontal="false"/> 

    <com.shirofuji.thrrededit.GL_handler
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/gl_layout"
        android:layout_alignParentLeft="true"
        android:layout_alignEnd="@id/listObjects"/>

</RelativeLayout>

MainActivity

package com.shirofuji.thrrededit;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.opengl.*;

public class MainActivity extends Activity
{
    GL_handler gl_maineditor;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        gl_maineditor = (GL_handler)findViewById(R.id.gl_layout);
        //layout_3d.addView(gl_maineditor);
        setContentView(R.layout.main);
    }

@Override
public void onResume(){
    super.onResume();
    gl_maineditor.onResume();
}

@Override
public void onPause(){
    super.onPause();
    gl_maineditor.onPause();
    }
}

GL_handler

package com.shirofuji.thrrededit;
import android.opengl.*;
import android.content.Context;
import android.view.View;
import android.util.*;

public class GL_handler extends View
{
    GLSurfaceView mainsurface;
    GL_renderer scene_renderer;

    public GL_handler(Context ctx,AttributeSet attr){
        super(ctx,attr);
        mainsurface = new GLSurfaceView(ctx);
        scene_renderer = new GL_renderer();

        mainsurface.setRenderer(scene_renderer);
    }
    public void onResume(){
        mainsurface.onResume();
    }
    public void onPause(){
        mainsurface.onPause();
    }
}

The order of calls in the onCreate() method of the activity is wrong:

gl_maineditor = (GL_handler)findViewById(R.id.gl_layout);
setContentView(R.layout.main);

setContentView() needs to be called before you can get any view with findByViewId() . The sequence should be:

setContentView(R.layout.main);
gl_maineditor = (GL_handler)findViewById(R.id.gl_layout);

With that out of the way, I think there's another aspect where you might be on the wrong track. Unless you have a master plan that is just not visible from the posted code. You're not placing a GLSurfaceView in your layout. The GL_handler you place in the layout derives from a plain View . It has a GLSurfaceView as a member variable, but that alone will not make the GLSurfaceView appear.

Unless you have a good reason, it should be much easier to derive your GL_handler class from GLSurfaceView . This will actually result in a GLSurfaceView that becomes part of your view hierarchy when the layout is inflated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM