【问题标题】:Problems with inflating a composite component that extends a Layoutclass扩展扩展 Layoutclass 的复合组件的问题
【发布时间】: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


    【解决方案1】:

    您需要按如下方式更改您的 DialPadView:

    <merge android:class="com.packageName.UIClassName"
        android:id="@+id/DialView"
        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"/>
    </merge>
    

    然后在你的构造函数中调用它:

    super(context, attrs, defStyle);
    
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    inflater.inflate(R.layout.DialView, this, true);
    

    然后您可以在主要活动中使用以下内容:

    DialView myDialView = new DialView(context);
    
    mainView.addView(myDialView);
    

    我希望这会有所帮助!

    【讨论】:

    • 感谢 Phoenixblade9 解决了我的问题。但是你不需要 android:class="..." 属性(eclipse 不会接受它)。
    • 很高兴听到它!很高兴我能帮忙:)
    猜你喜欢
    • 2017-10-02
    • 2011-09-20
    • 2020-10-22
    • 2011-07-25
    • 2017-10-13
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多