android – 自定义布局通货膨胀与Robolectric中的碎片无法正常工作
作者:互联网
在使用LayoutInflater对Fragment中的布局进行膨胀时,我收到此异常:
./res/layout/locations_list.xml line #-1 (sorry, not yet implemented): Error inflating class com.costum.android.widget.LoadMoreListView
我发现在充气自定义布局时会发生这种情况
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.locations_list, container, false);
}
编辑这是locations_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<com.costum.android.widget.LoadMoreListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp" />
</LinearLayout>
任何其他布局充气机工作,如在此测试中:
@Test
public void testInflator() {
ActivityController<SherlockFragmentActivity> activityController = CustomTestRunner
.startActivity();
SherlockFragmentActivity activity = activityController.get();
LayoutInflater from = LayoutInflater.from(activity);
View view = from
.inflate(com.twisper.R.layout.locations_list_item, null);
assertNotNull(view);
}
我正在使用带有2.2-SNAPSHOT的Robolectric,现在我的问题是如何解决这个问题或者如何实现缺少的功能,robolectric文档非常稀疏,因此我很难找到任何起点.
全栈跟踪
android.view.InflateException: XML file ./res/layout/locations_list.xml line #-1 (sorry, not yet implemented): Error inflating class com.costum.android.widget.LoadMoreListView
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
...
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:525)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createView(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createViewFromTag(LayoutInflater.java:687)
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)
...
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:252)
at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:188)
at org.robolectric.shadows.ShadowResources.access$000(ShadowResources.java:51)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:460)
我还在robolectrics问题跟踪器上发布了issue.
解决方法:
我想通了,如果你有这种情况,只需在XML中查找你的ID,即使这样
android:id="@+id/android:list"
在一些示例代码中经常可以看到它必须是:
android:id="@android:id/list"
标签:android,robolectric 来源: https://codeday.me/bug/20191008/1870106.html