我正在尝试将我的GLSurfaceView和其他UI元素一起设置为xml布局,并在LogCat中不断获得错误膨胀类com.vi.cubo01.MyGLSurfaceView。
以下是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"
/>
发布于 2014-03-14 16:09:44
如果要从XML中膨胀,则除了已经拥有的(Context)构造函数之外,还需要包含(Context,AttributeSet)构造函数。这是因为布局膨胀器需要调用它来处理您在XML中指定的属性,例如layout_width和layout_height。
public MyGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
setEGLContextClientVersion(2);
setRenderer(new CustomRenderer());
}发布于 2020-05-20 14:38:17
Kotlin版本:
class MyGLSurfaceView(context: Context,
attributes: AttributeSet? = null): GLSurfaceView(context, attributes) {
// ...
}https://stackoverflow.com/questions/22393845
复制相似问题