android中自定義view建構函式ContentItemView(Context context, AttributeSet paramAttributeSet)的用處

HighFUN發表於2013-08-25

自己定義一個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     <TextView
10             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     <TextView
17             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     <ImageView
23             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     <TextView
30             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     <LinearLayout
36             android:layout_width="fill_parent"
37             android:layout_height="2dp"
38             android:layout_gravity="center"
39             android:background="@color/black">
40     </LinearLayout>
41 
42     <TextView
43             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     <ScrollView
11             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

 

相關文章