【发布时间】:2011-10-24 09:56:35
【问题描述】:
我正在尝试扩展 LinearLayout 以用作复合组件。我已按照本教程进行操作: link
这看起来相当简单。但是,当我尝试为我的组件充气时,它总是失败。我从 logCat 得到的错误是找不到我的班级。我觉得很奇怪,据我所知,android 在它自己的组件中搜索我的组件(我不确定这里的正确措辞,请参见下文)。
我写的代码如下:
main(当前活动):
//just the ordinary autogenerated eclipse code...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
我的扩展线性布局:
public class DialPadView extends LinearLayout {
public DialPadView(Context ctx, AttributeSet attr) {
super(ctx, attr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
((Activity)getContext()).getLayoutInflater().inflate(R.layout.dialpadview, this);
}
}
我的 main.xml 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<whalenut.testinflate.DialPadView
android:id="@+id/mydialpad"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/></LinearLayout>
最后是与我的复合组件一起使用的 dialpadview.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<DialPadView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="test"/>
</DialPadView>
当然,我所有的类文件都驻留在包 whalenut.testinflate
中logCat 是这样说的:
java.lang.RuntimeException:无法启动活动 ComponentInfo{whalenut.testinflate/whalenut.testinflate.TestInflateActivity}:android.view.InflateException:二进制 XML 文件第 2 行:膨胀类 DialPadView 时出错
...
原因:android.view.InflateException: Binary XML file line #2: Error inflating class DialPadView
...
原因:java.lang.ClassNotFoundException: android.view.DialPadView in loader dalvik.system.PathClassLoader[/data/app/whalenut.testinflate-2.apk]
为什么 Dalvik(?) 当我在 xml-文件,是不是因为一开始就没有找到它?
简而言之,为什么我不能,或者更确切地说,我如何在 android 中扩展扩展布局?
【问题讨论】:
标签: android components composite