I've created a new customized view that is intended to replace the weights mechanism of linearLayout. I've added some styles attributes that can be used straight within the layout xml file.
The attrs.xml file contains:
<resources>
<declare-styleable name="WeightedLayout_LayoutParams">
<attr name="horizontalWeights" format="string" />
<attr name="verticalWeights" format="string" />
</declare-styleable>
</resources>
An example and full code can be seen here. The problem I'm facing is that in the visual editor, I keep getting a null pointer exception where I'm getting the string from the typedArray:
final TypedArray arr=context.obtainStyledAttributes(attrs,R.styleable.WeightedLayout_LayoutParams);
//...
final String horizontalWeights=arr.getString(R.styleable.WeightedLayout_LayoutParams_horizontalWeights);
The weird thing is that if i run the app, it runs fine (except for the weird bugs I've reported in the original thread). I've tried to modify the code of RomainGuy that has made a flowLayout , and I've noticed the same behavior occurs there too.
Can anyone please tell me what I should do? How come it doesn't work?
you should check TypedArray.hasValue() before assign :
final TypedArray arr = context.obtainStyledAttributes(attrs,R.styleable.WeightedLayout_LayoutParams);
//...
if (arr.hasValue(R.styleable.WeightedLayout_LayoutParams_horizontalWeights)) {
final String horizontalWeights = arr.getString(R.styleable.WeightedLayout_LayoutParams_horizontalWeights);
}