Android 膨胀GLSurfaceView时出错

Android 膨胀GLSurfaceView时出错,android,opengl-es,Android,Opengl Es,我试图在xml布局上设置我的GLSURFACHEVIEW以及其他UI元素,并在LogCat中不断膨胀类com.vi.cubo01.myGLSURFACHEVIEW时出错 以下是java代码: super.onCreate(savedInstanceState); mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.vistaGLSuperficie) setContentView(R.layout

我试图在xml布局上设置我的GLSURFACHEVIEW以及其他UI元素,并在LogCat中不断膨胀类com.vi.cubo01.myGLSURFACHEVIEW时出错

以下是java代码:

super.onCreate(savedInstanceState);

           mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.vistaGLSuperficie)
           setContentView(R.layout.activity_main);            
    }

    @Override
    protected void onResume() 
    {

            super.onResume();
            mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.vistaGLSuperficie);
            mGLSurfaceView.onResume();
    }

    @Override
    protected void onPause() 
    {
            super.onPause();
            mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.vistaGLSuperficie);
            mGLSurfaceView.onPause();
    }



    class MyGLSurfaceView extends GLSurfaceView {
        public MyGLSurfaceView(Context context) {
            super(context);
            setEGLContextClientVersion(2);
            setRenderer(new CustomRenderer());
        }
    }
以及xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textMultiLine" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0" />

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<com.vi.cubo01.MyGLSurfaceView
    android:id="@+id/vistaGLSuperficie"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     />


如果您是从XML膨胀,那么除了已有的(上下文)构造函数外,还需要包括(上下文,属性集)构造函数。这是因为布局充气器需要调用该属性来处理您在XML中指定的属性,例如布局宽度和布局高度

public MyGLSurfaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setEGLContextClientVersion(2);
    setRenderer(new CustomRenderer());
}

如果您是从XML膨胀,那么除了已有的(上下文)构造函数之外,还需要包括(上下文,AttributeSet)构造函数。这是因为布局充气器需要调用该属性来处理您在XML中指定的属性,例如布局宽度和布局高度

public MyGLSurfaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setEGLContextClientVersion(2);
    setRenderer(new CustomRenderer());
}
Kotlin版本:

class MyGLSurfaceView(context: Context,
                          attributes: AttributeSet? = null): GLSurfaceView(context, attributes) {
// ...
}
Kotlin版本:

class MyGLSurfaceView(context: Context,
                          attributes: AttributeSet? = null): GLSurfaceView(context, attributes) {
// ...
}

从您发布的代码来看,不清楚MyGLSurfaceView是否在活动类内部定义-请确保在活动类外部定义了它。另外,在onCreate()中,请确保仅在调用setContentView之后使用findViewById,而不是之前,否则将得到null。谢谢,在我从主活动中删除MyGLSurfaceView后,问题立即得到解决。从您发布的代码中,还不清楚MyGLSurfaceView是否定义在活动类内部-请确保在活动类外部定义。另外,在onCreate()中,请确保仅在调用setContentView之后而不是之前使用findViewById,否则将得到null。谢谢,在我从主活动中删除MyGLSurfaceView后,问题立即得到解决。