如何使用Robolectric 2.2测试ListView?它说android.widget.ListView尚未实现

时间:2013-12-14 15:24:15

标签: android listview android-listview robolectric

当我在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:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <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" />

</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <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" /> </LinearLayout>

这是加载activity_main.xml的onCreate()方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mListAdapter = new ArrayAdapter<String>(this, R.layout.route_item, R.id.list_item);
    setListAdapter(mListAdapter);

    showProgDialog(this, "Loading routes...");
    new GetRoutesTask().execute("http://" + getString(R.string.la_metro_query_host)
          + getString(R.string.la_metro_route_query));
}

2 个答案:

答案 0 :(得分:0)

失败的根本原因是:

java.lang.StringIndexOutOfBoundsException: String index out of range: -9

我怀疑Robolectric没有正确解析android:id引用。尝试将ID更改为项目中生成的内容,例如android:id="+id/list",并查看您的测试是否有效。

答案 1 :(得分:0)

正如Justin Muller评论我原来的问题,还有另一个问题正确地说明了ListView的id应该是

android:id="@android:id/list"