无法在布局XML中引用自定义视图
问题描述:
我想引用helloWorld XML布局中的自定义视图,但是我得到以下异常: 错误地夸大了类acme.my.MyTextView。无法在布局XML中引用自定义视图
但是,我能够实例化视图并将其手动添加到主内容视图。自定义视图是从它自己的XML布局构建的。我如何得到这个工作?
public class MyTextView extends LinearLayout {
MyTextView(Context context){
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.my_text_view,this);
}
MyTextView(Context context, AttributeSet attrs){
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.my_text_view,this);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<view class = "acme.my.MyTextView"
android:id="@+id/myView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
答
构造函数不公开。 :(
答
你要这样称呼它
<com.example.MyLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
也会使你的构造函数公共
@fredrick:通常情况下,有一个“所造成的”下面你举一个解释一个例外有关错误的更多内容 – CommonsWare 2010-07-29 04:37:28
MyTextView(Context,AttributeSet) 虽然我在构造函数的顶部有一个断点,但它永远不会被击中。 – Fredrick 2010-07-29 12:19:13
嗯,我用非常简单的布局完成了它,它似乎工作。弄清楚在更复杂的布局中打破了什么。 – Fredrick 2010-07-29 22:33:18