在我的活动中,我有一个自定义控件。在onCreate中,我将活动的视图正常膨胀:
setContentView(R.layout.image_viewer);
这是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<MyCustomImageView
android:id="@+id/tivImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
这是MyCustomTextView的简化代码:
public class MyCustomImageView extends ImageView
{
Context context;
public MyCustomImageView(Context context)
{
super(context);
sharedConstructing(context);
}
public MyCustomImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
sharedConstructing(context);
}
}
生成的异常读取:
android.view.InflateException: Binary XML file line #7: Error inflating class MyCustomImageView
答案 0 :(得分:4)
您需要添加包名称:
<com.my.package.MyCustomTextView
android:id="@+id/tivImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
答案 1 :(得分:0)
你必须创建调用超类构造函数的构造函数,因为它们将由inflater调用:
public class MyView extends View {
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
}
您必须将完整的包名称放在XML中:
<com.mypackagename.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />