我想为我的Android的RelativeLayout编写单元测试。目前,我的testcode设置情况如下:Android的RelativeLayout单元测试设置
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);
}
不过,我遇到NullPointerException异常与下面的堆栈跟踪
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)
我应该在我的设置更改()的代码,使测试运行正常吗?
这工作就像一个魅力对我来说: http://stackoverflow.com/a/6086510/1293704 – Primoz990 2012-12-13 11:41:04