通常我希望我可以在构造函数中使用layoutinflator(参见下面的错误)。但访问 mTitle 的唯一方法是使用 onFinishInflate 。这是正确的方法吗?
如果是这样,我在哪里可以在Android文档中找到它?
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<com.test.app.widgets.TestLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:id="@+id/main"
>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="11dp" />
</com.test.app.widgets.TestLayout>
和我的班级定义:
public class TestLayout extends RelativeLayout {
TextView mTitle;
public TestLayout(Context context) {
this(context, null, 0);
}
public TestLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TestLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
//Inflate here will result in ResourceNotFound
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (layoutInflater != null) {
RelativeLayout layout = (RelativeLayout) layoutInflater.inflate(R.id.main, this, false);
mTitle = (TextView) layoutInflater.inflate(R.id.title, this, false);
mTitle.setText("hioo");
}
}
@Override
protected void onFinishInflate() {
//This will work!
mTitle = (TextView)findViewById(R.id.title);
mTitle.setText("This is a test");
}
在另一个XML文件中,我使用以下方法加载此布局:
<include layout="@layout/TestLayout" android:id="@+id/test_layout"></include>
在构造函数中使用布局缓冲器时出错:
02-19 14:35:06.113: E/AndroidRuntime(6535): FATAL EXCEPTION: main
02-19 14:35:06.113: E/AndroidRuntime(6535): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.app/com.test.app.activity.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class com.test.app.widgets.TestLayout
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.os.Looper.loop(Looper.java:137)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-19 14:35:06.113: E/AndroidRuntime(6535): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 14:35:06.113: E/AndroidRuntime(6535): at java.lang.reflect.Method.invoke(Method.java:525)
02-19 14:35:06.113: E/AndroidRuntime(6535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-19 14:35:06.113: E/AndroidRuntime(6535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-19 14:35:06.113: E/AndroidRuntime(6535): at dalvik.system.NativeStart.main(Native Method)
02-19 14:35:06.113: E/AndroidRuntime(6535): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.test.app.widgets.TestLayout
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.view.LayoutInflater.parseInclude(LayoutInflater.java:816)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-19 14:35:06.113: E/AndroidRuntime(6535): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.app.Activity.setContentView(Activity.java:1895)
02-19 14:35:06.113: E/AndroidRuntime(6535): at com.test.app.activity.MainActivity.onCreate(MainActivity.java:87)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.app.Activity.performCreate(Activity.java:5133)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
02-19 14:35:06.113: E/AndroidRuntime(6535): ... 11 more
02-19 14:35:06.113: E/AndroidRuntime(6535): Caused by: java.lang.reflect.InvocationTargetException
02-19 14:35:06.113: E/AndroidRuntime(6535): at java.lang.reflect.Constructor.constructNative(Native Method)
02-19 14:35:06.113: E/AndroidRuntime(6535): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
02-19 14:35:06.113: E/AndroidRuntime(6535): ... 26 more
02-19 14:35:06.113: E/AndroidRuntime(6535): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f05001b type #0x12 is not valid
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2309)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.content.res.Resources.getLayout(Resources.java:934)
02-19 14:35:06.113: E/AndroidRuntime(6535): at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
02-19 14:35:06.113: E/AndroidRuntime(6535): at com.test.app.widgets.TestLayout.<init>(TestLayout.java:41)
02-19 14:35:06.113: E/AndroidRuntime(6535): at com.test.app.widgets.TestLayout.<init>(TestLayout.java:33)
02-19 14:35:06.113: E/AndroidRuntime(6535): ... 29 more