Take the 2-minute tour ×

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:

  <declare-styleable name="WeightedLayout_LayoutParams">
    <attr name="horizontalWeights" format="string" />
    <attr name="verticalWeights" format="string" />

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?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

finally i've found the answer , by looking at the android code at:


in order to get the text , you need to go over all of the attributes and use getText , for example :

final TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.WeightedLayout_LayoutParams);
int indexCount=a.getIndexCount();
for(int i=0;i<indexCount;++i)
  final int attr=a.getIndex(i);
share|improve this answer
add comment

Your Answer


