gpt4 book ai didi

android - 无法在 xml 布局中实例化自定义 View

转载 作者:行者123 更新时间:2023-11-30 03:50:43 25 4
gpt4 key购买 nike

我有这样一个类:

public class MyOpenGlView extends GLSurfaceView
{

public MyOpenGlView(Context context) {
super(context);
setRenderer(new MyRenderer()) ;
}
}

我想在我的 xml 布局中声明此类:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OpenGlActivity" >

<test.example.myfirstapp.OpenGlActivity.MyOpenGlView android:id = "@+id/myImage"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
/>

</LinearLayout>

但在“图形布局”模式下出现此错误:

The following classes could not be instantiated:
- test.example.myfirstapp.OpenGlActivity.MyOpenGlView (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.
Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse

有什么问题吗?

最佳答案

尝试设置 setRenderer(new MyRenderer()) ;在 isInEditMode() 下就像

if (isInEditMode())
{
setRenderer(new MyRenderer()) ;
}

同样在您的 XML 文件中尝试更改

<test.example.myfirstapp.OpenGlActivity.MyOpenGlView android:id = "@+id/myImage"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
/>

 <MyOpenGlView android:id = "@+id/myImage"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
/>

如果你正在使用android studio并让它自动导入类

否则尝试将其更改为

<view
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="test.example.myfirstapp.OpenGlActivity.MyOpenGlView"
android:id="@+id/myImage" />

关于android - 无法在 xml 布局中实例化自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14196558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com