Android 在布局XML中引用自定义视图时出现问题
我试图引用helloWorld XML布局中的自定义视图,但出现以下异常: 膨胀类acme.my.MyTextView时出错 但是,我能够实例化视图并手动将其添加到主内容视图中。自定义视图是从它自己的XML布局构建的。我怎样才能让它工作Android 在布局XML中引用自定义视图时出现问题,android,android-layout,custom-view,Android,Android Layout,Custom View,我试图引用helloWorld XML布局中的自定义视图,但出现以下异常: 膨胀类acme.my.MyTextView时出错 但是,我能够实例化视图并手动将其添加到主内容视图中。自定义视图是从它自己的XML布局构建的。我怎样才能让它工作 public class MyTextView extends LinearLayout { MyTextView(Context context){ super(context); LayoutInflater layoutInflater
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"
/>
公共类MyTextView扩展了LinearLayout{
MyTextView(上下文){
超级(上下文);
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
LayoutFlater.充气(R.layout.my_text_视图,此);
}
MyTextView(上下文、属性集属性){
超级(上下文,attrs);
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
LayoutFlater.充气(R.layout.my_text_视图,此);
}
}
构造函数不是公共的。
:(你必须这样称呼它
<com.example.MyLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
还要公开你的构造函数@fredrick:通常,在你引用的构造函数下面会有一个“由引起”的异常,可以解释更多关于出错原因的信息。MyTextView(上下文,属性集)我在构造器的顶部有一个断点,但它从来没有被击中。我用非常简单的布局做到了这一点,它似乎可以工作。我将在更复杂的布局中找出打破它的原因。