在xml中引用自定义view
方法一:
<com.test.copytext.CopyText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
方法二:
<view class="com.test.copytext.CopyText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="aaaaaaaaaaaa"
/>
在xml文件中间引用自定义view时遇到了这个错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{cm.test/cm.test.TestsActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.test.testview
这就需要查看在xml中引用时的包名是否正确
还可能会遇到这个错:11-24 10:58:38.993: ERROR/AndroidRuntime(323): Caused by: java.lang.NoSuchMethodException: HelloView(Context,AttributeSet)
这是因为自定义View中缺少了一个构造方法
public HelloView(Context context,AttributeSet attrs){
super(context, attrs);
}
本文介绍了在XML布局文件中如何正确引用自定义View的方法,并列举了常见的错误及其解决方式,包括检查包名正确性和确保自定义View具备必要的构造方法。
3359





