我定义了一个自定义ViewGroup,它扩展了LinearLayout的功能:
public class TestLayout extends LinearLayout { public TestLayout(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.testlayout,this,true); } }
它膨胀的布局(testlayout.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" android:gravity="center"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button"/> </LinearLayout>
最后我在main.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" > <my.test.namespace.TestLayout android:id="@+id/testLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" > </my.test.namespace.TestLayout> </LinearLayout>
在布局编辑器中查看我的main.xml时,eclipse会抛出一个错误:
my.test.namespace.TestLayout无法实例化.
和堆栈跟踪:
android.content.res.Resources$NotFoundException: Could not resolve resource value: 0x7F030001. at com.android.layoutlib.bridge.android.BridgeResources.throwException(BridgeResources.java:648) at com.android.layoutlib.bridge.android.BridgeResources.getLayout(BridgeResources.java:270) at android.view.LayoutInflater.inflate(LayoutInflater.java:318) at my.test.namespace.TestLayout.<init>(TestLayout.java:18) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:397) at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:165) at com.android.layoutlib.bridge.android.BridgeInflater.loadCustomView(BridgeInflater.java:205) at com.android.layoutlib.bridge.android.BridgeInflater.createViewFromTag(BridgeInflater.java:133) at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
0x7F030001指向R.java中的布局xml文件.我试着清理我的项目,但没有做任何事情.我使用LayoutInflater错了,还是这里发生了什么?