views:

426

answers:

0

I'm trying to build a compound control in Android, containing (among other things) a ScrollView. Things go wrong when I try to view the control in Eclipse, crashing with a NullPointerException after the error message: "Parser is not a BridgeXmlBlockParser".

Stacktrace:

java.lang.NullPointerException
at android.view.View.<init>(View.java:1720)
at android.view.ViewGroup.<init>(ViewGroup.java:277)
at android.widget.FrameLayout.<init>(FrameLayout.java:83)
at android.widget.ScrollView.<init>(ScrollView.java:128)
at android.widget.ScrollView.<init>(ScrollView.java:124)
at android.widget.ScrollView.<init>(ScrollView.java:120)
at my.compound.control.StringPicker.onMeasure(StringPicker.java:46)
...

I've traced the error to the following conditions:

  • The NPE is thrown because a Context.obtainStyledAttributes() call returns null when the attrs argument passed is null.
  • This only applies to the BridgeContext implementation used in Eclipse, which expects attrs to be an instance of the BridgeXmlBlockParser.
  • The attrs argument is null because I create the ScrollView using the (Context) constructor.

There is a workaround of course, which is passing the attrs I receive when Eclipse constructs the compound control, but I don't want all the attributes set on the compound control to apply to my inner control.

Am I doing something wrong, is this a bug in Android Eclipse, ...?

This is what my.compound.control.StringPicker.onMeasure looks like (stripped it a bit for clarity):

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (this.getChildCount() != requestedLength) {
        this.removeAllViews();
        int childWidth = getWidth() / requestedLength;
        int childHeight = getHeight();
        for (int i = 0; i < requestedLength; i++) {
            ScrollView child = new ScrollView(getContext());
            child.setLayoutParams(new LayoutParams(childWidth, childHeight));
            addView(child);
        }
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}