tags:

views:

116

answers:

1

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 ?

+1  A: 

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?

Rich
Thanks, indeed i missed the layout params.After adding: testView.setLayoutParams(new LayoutParams(500,500));the code runs properly :-)
dqminh