【问题标题】:custom view cannot be instantiated in xml layout自定义视图无法在 xml 布局中实例化
【发布时间】:2013-01-07 13:03:07
【问题描述】:

我有这样的课:

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

有什么问题?

【问题讨论】:

  • 你确定你放的是正确的包吗?
  • 是的,包是:test.example.myfirstapp,活动是:OpenGlActivity,类是:MyOpenGlView,

标签: android xml android-layout android-custom-view glsurfaceview


【解决方案1】:

试着把 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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 2011-03-16
    • 2012-07-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多