android中自定义view构造函数ContentItemView(Context context, AttributeSet paramAttributeSet)的用处
来源:互联网 发布:java接口开发 编辑:程序博客网 时间:2024/03/28 22:10
自己定义一个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
- android中自定义view构造函数ContentItemView(Context context, AttributeSet paramAttributeSet)的用处
- Android中SQLite构造函数参数Context的几点注意事项
- Android中,Context,什么是Context?
- Android中,Context,什么是Context?
- android中context的理解
- android中context的使用
- android中context的详解
- Android中Context的理解
- android中Context的作用
- Android中Context的传递
- Android中Context的含义
- Android中Context的使用
- Android中 Context的用法
- Android自定义View构造函数
- Android中Context的作用以及Context的详细用法
- Android中Context详解 ---- 你所不知道的Context
- Android中Context详解 ---- 你所不知道的Context
- Android中Context详解 ---- 你所不知道的Context
- Linux文件系统结构
- hdu4611 Balls Rearrangement(推公式)
- Bmp图形用StretchBlt缩小,图形质量严重损失?
- cocos2d_字体
- Linux Shell脚本编程--Head/Tail命令详解
- android中自定义view构造函数ContentItemView(Context context, AttributeSet paramAttributeSet)的用处
- iOS应用性能调优的25个建议和技巧
- linux中Find命令的使用
- hdu 1556
- UVA 10026 Shoemaker's Problem 鞋匠的难题 贪心+排序
- _tcschrs,_tcslen,WideCharToMultiByte,MultiByteToWideChar,mbstowcs,wcstombs字符串操作
- POJ 1045
- 广度优先搜索
- ARM中的MOV指令