当我在我的 ListActivity 上使用 Robolectric 2.2 运行 JUnit 测试时,我收到一个 InflateException,指出 ListView 未实现。我该如何解决这个问题?我一直在调查使用 .shadowOf() 的方法,但我不确定该怎么做。
这是堆栈跟踪:
android.view.InflateException: XML file ./res/layout/activity_main.xml line #-1 (sorry, not yet implemented): Error inflating class android.widget.ListView at android.view.LayoutInflater.createView(LayoutInflater.java:613) at org.robolectric.shadows.RoboLayoutInflater.onCreateView(RoboLayoutInflater.java:38) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:396) at android.view.LayoutInflater.inflate(LayoutInflater.java:352) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256) at android.app.Activity.setContentView(Activity.java:1867) at com.jch.latransit.MainActivity.onCreate(MainActivity.java:43) at android.app.Activity.performCreate(Activity.java:5008) at org.fest.reflect.method.Invoker.invoke(Invoker.java:112) at org.robolectric.util.ActivityController$1.run(ActivityController.java:116) at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256) at org.robolectric.util.ActivityController.create(ActivityController.java:111) at org.robolectric.util.ActivityController.create(ActivityController.java:123) at com.jch.latransit.domain.TestTimeStamp.setUp(TestTimeStamp.java:33) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createView(LayoutInflater.java:587) at android.view.LayoutInflater.createView(LayoutInflater.java) at org.robolectric.shadows.RoboLayoutInflater.onCreateView(RoboLayoutInflater.java:38) at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_onCreateView(LayoutInflater.java:660) at android.view.LayoutInflater.onCreateView(LayoutInflater.java) at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createViewFromTag(LayoutInflater.java:685) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java) at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_rInflate(LayoutInflater.java:746) at android.view.LayoutInflater.rInflate(LayoutInflater.java) at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java) at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:396) at android.view.LayoutInflater.inflate(LayoutInflater.java) at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:352) at android.view.LayoutInflater.inflate(LayoutInflater.java) at com.android.internal.policy.impl.PhoneWindow.$$robo$$PhoneWindow_1a87_setContentView(PhoneWindow.java:256) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java) at android.app.Activity.$$robo$$Activity_c57b_setContentView(Activity.java:1867) at android.app.Activity.setContentView(Activity.java) at com.jch.latransit.MainActivity.onCreate(MainActivity.java:43) at android.app.Activity.$$robo$$Activity_c57b_performCreate(Activity.java:5008) at android.app.Activity.performCreate(Activity.java) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.fest.reflect.method.Invoker.invoke(Invoker.java:112) at org.robolectric.util.ActivityController$1.run(ActivityController.java:116) at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256) at org.robolectric.util.ActivityController.create(ActivityController.java:111) at org.robolectric.util.ActivityController.create(ActivityController.java:123) at com.jch.latransit.domain.TestTimeStamp.setUp(TestTimeStamp.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) ... 23 more Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -9 at java.lang.String.substring(String.java:1911) at org.robolectric.res.ResName.qualifyResName(ResName.java:50) at org.robolectric.res.Attribute.getResourceReference(Attribute.java:138) at org.robolectric.res.Attribute.qualifiedValue(Attribute.java:127) at org.robolectric.res.builder.XmlFileBuilder$XmlResourceParserImpl.qualify(XmlFileBuilder.java:316) at org.robolectric.res.builder.XmlFileBuilder$XmlResourceParserImpl.getAttributeValue(XmlFileBuilder.java:340) at org.robolectric.shadows.ShadowResources.findAttributeValue(ShadowResources.java:257) at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:193) at org.robolectric.shadows.ShadowResources.access$000(ShadowResources.java:52) at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:465) at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java) at android.content.Context.obtainStyledAttributes(Context.java:374) at android.view.View.__constructor__(View.java:3297) at org.fest.reflect.method.Invoker.invoke(Invoker.java:112) at org.robolectric.shadows.ShadowView.__constructor__(ShadowView.java:61) at android.view.View.(View.java:3295) at android.view.ViewGroup.(ViewGroup.java:427) at android.widget.AdapterView.(AdapterView.java:235) at android.widget.AbsListView.(AbsListView.java:758) at android.widget.ListView.(ListView.java:141) at android.widget.ListView.(ListView.java:137) at android.view.LayoutInflater.createView(LayoutInflater.java:587) at org.robolectric.shadows.RoboLayoutInflater.onCreateView(RoboLayoutInflater.java:38) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:396) at android.view.LayoutInflater.inflate(LayoutInflater.java:352) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256) at android.app.Activity.setContentView(Activity.java:1867) at com.jch.latransit.MainActivity.onCreate(MainActivity.java:43) at android.app.Activity.performCreate(Activity.java:5008) at org.fest.reflect.method.Invoker.invoke(Invoker.java:112) at org.robolectric.util.ActivityController$1.run(ActivityController.java:116) at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256) at org.robolectric.util.ActivityController.create(ActivityController.java:111) at org.robolectric.util.ActivityController.create(ActivityController.java:123) at com.jch.latransit.domain.TestTimeStamp.setUp(TestTimeStamp.java:33) ... 23 more
这是我的 activity_main.xml:
< ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:fastScrollEnabled="true">
ListView >
< TextView
android:id="@+id/android:empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
线性布局>
这里是加载activity_main.xml的onCreate()方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListAdapter = new ArrayAdapter(this, R.layout.route_item, R.id.list_item);
setListAdapter(mListAdapter);
showProgDialog(this, "加载路线...");
新的 GetRoutesTask().execute("http://"+ getString(R.string.la_metro_query_host)
+ getString(R.string.la_metro_route_query));
}
最佳答案
失败的根本原因是这样的:
java.lang.StringIndexOutOfBoundsException: String index out of range: -9
我怀疑 Robolectric 没有正确解析 android:id
引用。尝试将 id 更改为项目中生成的内容,例如 android:id="+id/list"
并查看您的测试是否有效。
关于android - 如何使用 Robolectric 2.2 测试 ListView?它说 android.widget.ListView 还没有实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20584985/