【问题标题】:Android - Views in Custom Compound Component are not inflated (findByView returns null)Android - 自定义复合组件中的视图未膨胀(findByView 返回 null)
【发布时间】:2010-06-12 10:30:11
【问题描述】:

我在 XML 中制作了一个自定义组件,由一个顶部堆叠有图像视图的按钮组成:

<myapp.widget.ClearableCaptionedButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
    android:id="@+id/ccbutton_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|left"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:background="@android:drawable/edit_text"/>
<ImageView
    android:id="@+id/ccbutton_clear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dip"
    android:layout_alignRight="@id/ccbutton_button"
    android:layout_alignTop="@id/ccbutton_button"
    android:layout_alignBottom="@id/ccbutton_button"/>
  </myapp.widget.ClearableCaptionedButton>

java源代码摘录:

public class ClearableCaptionedButton extends RelativeLayout implements OnClickListener {
...
public ClearableCaptionedButton(Context context, AttributeSet attrs) {
    super(context, attrs);
// some stuff that works fine
}
..

protected void onFinishInflate() {
  super.onFinishInflate();

  mButton = (Button) findViewById(R.id.ccbutton_button);
  mClear = (ImageView) findViewById(R.id.ccbutton_clear);

  mButton.setText("");  // error here: mButton == null
}

我的问题类似于this 之一。当我尝试在自定义化合物中查找视图时,findViewById 返回 null。但是,如您所见,我已经添加了 super(context, attrs);给构造函数。 我在 xml 布局中直接使用自定义组件,如下所示:

<LinearLayout>
<!-- some stuff -->
<myapp.widget.ClearableCaptionedButton
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    app:caption="to"/>
</LinearLayout>

有人能发现什么吗?谢谢。

【问题讨论】:

  • 我编辑了我的帖子并将包的名称更改为 myapp.widget 以避免误解。

标签: android android-relativelayout


【解决方案1】:

我对你的两个 XML 布局感到困惑。

第二个是你说你使用小部件的地方。因此,我假设名为ClearableCaptionedButton 的类在包de.pockettaxi.widget 中。

我进一步假设您的第一个布局中显示的 Button 和 ImageView 是始终应该在 ClearableCaptionButton 中的东西,而不是 ClearableCaptionButton 的重用者提供的东西。

如果这些假设都是正确的,那么你有两个问题。

  • 首先,您没有使用第一个 布局在我能看到的任何地方。
  • 二、第一 布局引用了可能不存在的myapp.widget.ClearableCaptionedButton。

我会将myapp.widget.ClearableCaptionedButton 元素替换为&lt;merge&gt; 元素,然后在构造函数或ClearableCaptionedButton 的onFinishInflate() 中扩展该布局。

Here is a sample project 来自我的一本书,其中展示了以这种方式工作的自定义小部件的使用。

另外,鉴于你的包裹名称,我希望它要么是一个非常大的口袋,要么是一辆非常小的出租车...... :-)

【讨论】:

  • 好的,我会这样尝试。我已经考虑过手动膨胀 xml。问题是,我尝试以与某些 android 应用程序相同的方式进行操作。如果您查看 android 源代码中提供的 DialtactsActivity 中的“KindSectionView”,它们(afaics)不使用充气器,而只是使用与自定义组件同名的元素的 xml 布局。实际上,这对于我制作的另一个组件来说效果很好,但是在那里,我只从源代码创建它,而不是从 xml。
  • @Julian Arz:据说,有一些神奇的方法可以让它自动膨胀,但我从来没有让它工作,无论如何它也不会节省那么多代码。
  • 你的示例项目和这篇文章也帮助我修复了我的布局。在我按照您的示例进行操作之前,我的构造函数有问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
相关资源
最近更新 更多