[英]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. 我想创建一个应用程序,其中glsurfaceview与listview一起位于相对布局中。 While coding, my IDE shows no errors, but once I run the application it just crashes.
编码时,我的IDE没有显示任何错误,但是一旦我运行应用程序,它就会崩溃。
main.xml 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 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: 活动的
onCreate()
方法中的调用顺序是错误的:
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()
. 在使用
findByViewId()
获取任何视图之前,需要调用setContentView()
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. 您没有在布局中放置
GLSurfaceView
。 The GL_handler
you place in the layout derives from a plain View
. 放置在布局中的
GL_handler
源自普通View
。 It has a GLSurfaceView
as a member variable, but that alone will not make the GLSurfaceView
appear. 它有一个
GLSurfaceView
作为成员变量,但仅此一项不会使GLSurfaceView
出现。
Unless you have a good reason, it should be much easier to derive your GL_handler
class from GLSurfaceView
. 除非你有充分的理由,否则从
GLSurfaceView
派生你的GL_handler
类要容易GLSurfaceView
。 This will actually result in a GLSurfaceView
that becomes part of your view hierarchy when the layout is inflated. 这实际上会导致
GLSurfaceView
在布局膨胀时成为视图层次结构的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.