Programmatically creating scrollview (s) from a custom component in android

I am trying to create a composite control in Android containing (among other things) a ScrollView. Everything happens incorrectly when I try to view the control in Eclipse, it fails with a NullPointerException after the error message: "Parser is not 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 traced the error to the following conditions:

  • NPE is thrown because the call Context.obtainStyledAttributes()returns nullwhen the argument passed attrsis equal null.
  • This only applies to the implementation BridgeContextused in Eclipse, which expects to attrsbe an instance BridgeXmlBlockParser.
  • An argument attrs nullbecause I create a ScrollView using the constructor (Context).

, , , attrs, Eclipse , , , , .

- , Android Eclipse,...?

, my.compound.control.StringPicker.onMeasure( ):

@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()); // NPE here
            child.setLayoutParams(new LayoutParams(childWidth, childHeight));
            addView(child);
        }
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
+3
2

, Android.

Android 2.3 , Android 2.2 . Android ( ), attrs .
, Eclipse , .

0

, XML- ? , , , XML, , , StringPicker ( , AttributeSet attrs). super (context, attrs).

+1

Source: https://habr.com/ru/post/1746561/


All Articles