我想爲我的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