扩展扩展 Layoutclass 的复合组件时出现问题

发布于 2024-11-29 02:25:00 字数 2501 浏览 0 评论 0原文

我正在尝试扩展 LinearLayout 以用作复合组件。我已经遵循了这个教程: 链接

这看起来相当直接向前。然而,当我尝试给我的组件充气时,它总是失败。我从 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:二进制XML文件行#2:错误膨胀类 DialPadView

...

引起:java.lang.ClassNotFoundException:加载程序中的 android.view.DialPadView dalvik.system.PathClassLoader[/data/app/whalenut.testinflate-2.apk]

为什么 Dalvik(?) 在包 android.view 中搜索我的类而不是 whalenut.testinflate 当我在 xml 文件中给出完全限定的类名时,是否是因为它一开始就没有找到它?

简而言之,为什么我不能,或者更确切地说,我如何在 android 中扩展扩展布局?

I'm trying to extend a LinearLayout to use as a composite component. I have followed this tutorial:
link

Which seems fairly straight forward. However when I try to inflate my component it fails all the time. The error I get from logCat is that my class can't be found. It seems strange to me that as far as I can see android searches for my component among it's own components (I'm unsure about the correct wording here, please se below).

The code I have written is as follows:

main (the current activity):

//just the ordinary autogenerated eclipse code...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

My extended linear layout:

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);
}

}

My main.xml layout file

<?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>

and finally my dialpadview.xml file to use with my composite component:

<?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>

ofcourse all my class files resides in the package whalenut.testinflate

logCat says this:

java.lang.RuntimeException: Unable to start activity ComponentInfo{whalenut.testinflate/whalenut.testinflate.TestInflateActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class DialPadView

...

Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class DialPadView

...

Caused by: java.lang.ClassNotFoundException: android.view.DialPadView in loader dalvik.system.PathClassLoader[/data/app/whalenut.testinflate-2.apk]

Why is Dalvik(?) searching for my class in the package android.view instead of whalenut.testinflate when I have given the fully qualified classname in the xml-file, is it because it doesn't find it there to begin with?

In short why can't I, or rather how do I inflate a extended Layout in android?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里人 2024-12-06 02:25:00

您需要按如下方式更改 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);

我希望这有帮助!

You need to change your DialPadView as follows:

<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>

Then call this in your constructor:

super(context, attrs, defStyle);

LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

inflater.inflate(R.layout.DialView, this, true);

You can then use the following in your main activity:

DialView myDialView = new DialView(context);

mainView.addView(myDialView);

I hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文