开发者

Android's RelativeLayout Unit Test setup

i'm trying to write an unit test for my Android's RelativeLayout. Currently, my testco开发者_开发知识库de setup is as follow:

public class SampleRelativeLayoutTest extends AndroidTestCase {

private ViewGroup testView;

private ImageView icon;
private TextView  title;


@Override
protected void setUp() throws Exception {
    super.setUp();
    // inflate the layout
    final Context context = getContext();
    final LayoutInflater inflater = LayoutInflater.from(context);
    testView = (ViewGroup) inflater.inflate(R.layout.sample_layout,
            null);
    // manually measure and layout
    testView.measure(500, 500);
    testView.layout(0, 0, 500, 500);
    // init variables
    icon = (ImageView) testView.findViewById(R.id.icon);
    title = (TextView) testView.findViewById(R.id.title);
}

However, I encountered NullPointerException with the following stack trace

java.lang.NullPointerException
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:427)
at android.view.View.measure(View.java:7964)
at com.dqminh.test.view.SampleRelativeLayoutTest.setUp(SampleRelativeLayoutTest.java:33)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)

What should I change in my setUp() code to make the test run properly ?


I've got a copy of the Android source, but not sure if it's the same as what you're on (or maybe for this class it doesn't matter). The exception is a NullPointerException at line 427 of RelativeLayout inside the onMeasure method. That line in the version I have is the first access of mLayoutParams instance variable in the onMeasure method. Is it possible you forgot to add the layout_width and layout_height params in your xml layout or something like that?


Also works if you wrap your RelativeLayout inside a LinearLayout. and pass the Linearlayout instead.


I will do this an works:

LayoutParams layoutParams = new LayoutParams(500,500);
testView.setLayoutParams(layoutParams);
testView.layout(0, 0, 500, 500);


Another possible reason is that : you have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.

Below is the annotation of RelativeLayout:

A Layout where the positions of the children can be described in relation to each other or to the parent. For the sake of efficiency, the relations between views are evaluated in one pass, so if view Y is dependent on the position of view X, make sure the view X comes first in the layout.

Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.

Also see RelativeLayout.LayoutParams for the layout attributes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜