开发者

Android - Views in Custom Compound Component are not inflated (findByView returns null)

I have made a Custom Component in XML, consisting of a button with an imageview stacked on top of it:

<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_align开发者_高级运维Top="@id/ccbutton_button"
    android:layout_alignBottom="@id/ccbutton_button"/>
  </myapp.widget.ClearableCaptionedButton>

extract of java source code:

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
}

My problem is similar to this one. When i try to find the views inside the custom compound, findViewById returns null. But, as you can see, i already added super(context, attrs); to the constructor. i am using the custom component directly in xml layout, like this:

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

can anybody spot something? thanks.


I am confused by your two XML layouts.

The second one is where you say you use the widget. Hence, I am assuming that the class named ClearableCaptionedButton is in the package de.pockettaxi.widget.

I am further assuming that the Button and ImageView shown in your first layout are things that are always supposed to be in ClearableCaptionButton, not something supplied by the reuser of ClearableCaptionButton.

If those assumptions are all correct, then you have two problems.

  • First, you aren't using the first layout anywhere that I can see.
  • Second, the first layout has reference to a myapp.widget.ClearableCaptionedButton that probably does not exist.

I would replace the myapp.widget.ClearableCaptionedButton element with a <merge> element, then inflate that layout in the constructor or onFinishInflate() of ClearableCaptionedButton.

Here is a sample project from one of my books that shows the use of a custom widget that works in this manner.

Also, given your package name, I hope that it is either a very large pocket or a very small taxi... :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜