Android's RelativeLayout Unit Test setup

Posted by dqminh on Stack Overflow See other posts from Stack Overflow or by dqminh
Published on 2010-03-20T03:43:58Z Indexed on 2010/03/20 3:51 UTC
Read the original article Hit count: 674

Filed under:

i'm trying to write an unit test for my Android's RelativeLayout. Currently, my testcode 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 ?

© Stack Overflow or respective owner

Related posts about android