【发布时间】:2010-07-29 03:38:11
【问题描述】:
我试图在 helloWorld XML 布局中引用自定义视图,但出现以下异常: 膨胀类 acme.my.MyTextView 时出错。
但是,我可以实例化视图并将其手动添加到主内容视图中。自定义视图是从它自己的 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"
/>
【问题讨论】:
-
@fredrick:通常情况下,在您引用的异常下方有一个“由”异常,可以更多地解释出了什么问题。
-
MyTextView(Context,AttributeSet) 我在构造函数的顶部有一个断点,它永远不会被命中。
-
嗯,我用非常简单的布局做到了这一点,它似乎工作。我会弄清楚是什么在更复杂的布局中破坏了它。
标签: android android-layout custom-view