android中自定义view构造函数ContentItemView(Context context, AttributeSet paramAttributeSet)的用处
自己定义一个view
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:background="@color/white"> 8 9 <TextView10 android:layout_width="match_parent"11 android:layout_height="wrap_content"12 android:id="@+id/textViewTitle"13 android:textColor="@color/black"14 android:gravity="center" android:textSize="26dp"/>15 16 <TextView17 android:layout_width="match_parent"18 android:layout_height="wrap_content"19 android:id="@+id/textViewAuthor"20 android:layout_gravity="left" android:textColor="@android:color/darker_gray" android:textSize="16dp"/>21 22 <ImageView23 android:layout_width="wrap_content"24 android:layout_height="wrap_content"25 android:id="@+id/imageView"26 android:layout_gravity="center_horizontal"27 android:scaleType="center"/>28 29 <TextView30 android:layout_width="match_parent"31 android:layout_height="wrap_content"32 android:id="@+id/textViewContent"33 android:layout_gravity="center_horizontal" android:textColor="@color/black" android:textSize="20dp"/>34 35 <LinearLayout36 android:layout_width="fill_parent"37 android:layout_height="2dp"38 android:layout_gravity="center"39 android:background="@color/black">40 </LinearLayout>41 42 <TextView43 android:layout_width="match_parent"44 android:layout_height="wrap_content"45 android:id="@+id/textViewOtherInfo"46 android:layout_gravity="left" android:clickable="true" android:textColor="@android:color/darker_gray"47 android:textSize="16dp"/>48 </LinearLayout>
对应的类
1 public class ContentItemView extends LinearLayout { 2 3 private TextView title; 4 private TextView author; 5 private TextView content; 6 private TextView otherInfo; 7 private ImageView contentImage; 8 9 private ContentInfo info;10 11 public ContentItemView(Context context) {12 super(context);13 init(context);14 }15 16 private void init(Context context) {17 LinearLayout convertView =18 (LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null);19 title = (TextView) convertView.findViewById(R.id.textViewTitle);20 author = (TextView) convertView.findViewById(R.id.textViewAuthor);21 content = (TextView) convertView.findViewById(R.id.textViewContent);22 otherInfo = (TextView) convertView.findViewById(R.id.textViewOtherInfo);23 contentImage = (ImageView) convertView.findViewById(R.id.imageView);24 }25 }
添加到一个activity中
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:background="@color/white" android:paddingLeft="12dp" android:paddingTop="12dp" 8 android:paddingRight="12dp"> 9 10 <ScrollView11 android:layout_width="match_parent"12 android:layout_height="match_parent"13 android:id="@+id/scrollView">14 15 <view android:layout_width="match_parent"16 android:layout_height="match_parent"17 class="com.HighFunStudio.shudu.ContentItemView" android:id="@+id/view"/>18 </ScrollView>19 </LinearLayout>
运行,提示错误:
08-25 14:58:28.165: ERROR/AndroidRuntime(1342): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.HighFunStudio.shudu/com.HighFunStudio.shudu.ArticleActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class com.HighFunStudio.shudu.ContentItemView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
添加一个构造函数
1 public ContentItemView(Context context, AttributeSet paramAttributeSet) {2 super(context, paramAttributeSet);3 init(context);4 }
哦了,一切正常。但是原因是什么?
断点调试,看到调用栈
从
void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs,
boolean finishInflate) throws XmlPullParserException, IOException
中创建一个view,最后调用函数调用的是带属性的构造函数来创建一个view
具体的调用栈如下
<1> main@830013385120, prio=5, in group 'main', status: 'RUNNING'
at com.HighFunStudio.shudu.ContentItemView.<init>(ContentItemView.java:51)
at java.lang.reflect.Constructor.constructNative(Constructor.java:-1)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
at android.app.Activity.setContentView(Activity.java:1881)
at com.HighFunStudio.shudu.ArticleActivity.onCreate(ArticleActivity.java:48)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Method.java:-1)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(NativeStart.java:-1)
所以只有创建
public ContentItemView(Context context, AttributeSet paramAttributeSet)
这样的构造函数才能够正确的使用自定义view
更多相关文章
- C语言函数的递归(上)
- android 自定义属性
- Android(安卓)NDK纯C++开发(2)
- TextView设置android:textAllCaps="true"带来的问题
- Android(安卓)5.0以上版本去掉Button自带阴影效果的方法
- android animator
- Android(安卓)启动分析-init进程&init.rc
- Android的资源获取的相关方法
- Android(安卓)Vold 分析(一)--system/vold/main.cpp-----mian函数