Android Robolectric中带有碎片的自定义布局膨胀不起作用

Android Robolectric中带有碎片的自定义布局膨胀不起作用,android,robolectric,Android,Robolectric,使用LayoutFlater在片段中膨胀布局时,我遇到以下异常: ./res/layout/locations_list.xml line #-1 (sorry, not yet implemented): Error inflating class com.costum.android.widget.LoadMoreListView 我发现这是在中膨胀自定义布局时发生的 @Override public View onCreateView(LayoutInflater inflat

使用LayoutFlater在片段中膨胀布局时,我遇到以下异常:

 ./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);
    }
编辑这是位置列表.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>

我也在robolectrics问题跟踪器上发布了这篇文章。

我发现了这一点,如果你有这样的库存,只需在XML中查找你的ID,即使

 android:id="@+id/android:list"
在一些示例代码中经常可以看到,它必须是:

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

在我的情况下,当我的应用程序中出现此问题时,我遇到了此问题:

    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>support-v4</artifactId>
        <version>r13</version>
    </dependency>

com.google.android
支持-v4
r13
但在我的unittest项目中:

    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>support-v4</artifactId>
        <version>r7</version>
    </dependency>

com.google.android
支持-v4
r7

我在测试中使用的旧版本的支持库不包含我试图使用的类(android.support.v4.widget.DrawerLayout)。

在我的例子中,我的
FragmentActivity
XML正在膨胀一个
片段,该片段需要一个
捆绑包。我将XML替换为
FrameLayout
holder,并在活动中添加了带有适当捆绑包的片段。

Robolectric(3.0-Snapshot)的最新版本在自定义视图方面存在一些问题

要解决此问题,请执行以下操作:

  • 在代码所在的模块应用程序中,创建一个名为project.properties的文件,其级别与AndroidManifest.xml相同
  • 参考build的类文件夹填充内容。e、 g.对于交错边缘视图:
  • android.library.reference.1=../../build/intermediates/aar/com.etsy.android.grid/library/1.0.5

    在这里,您必须检查三件事:

    • 每个引用应该有一行
    • 每个参考都有一个数字(1、2、3),每次增加一个
    • 末尾的版本号文件夹必须与build.gradle文件中的版本号匹配
    这里有一个项目工作的例子:

    post locations\u list.xml追加缺少的文件。您确定您有com.costum.android.widget.LoadMoreListView类吗?也许“costum”中有错别字?嗯,是错别字,但不是我的错别字。它是正确的组id,在Android上运行它是有效的。我不明白,所以它是否有效?您引用的组id是什么?通常,我会这样给出资源标识符,android:id=“@+id/txtViewOne”。那么,应该如何格式化以克服这个问题呢?我得到编译错误,如果我改为android:id=“android:id/txtViewOne”android:id=“@+id/txtViewOne”就可以了,上面的例子就是一个特殊的例子。@+id/android:。。。前缀在以前的API版本中使用,与robolectric不兼容。@jrhamza:您需要添加@like this:
    android:id=“@android:id/txtViewOne”
    我不认为这是解决我的问题的方法,但确实如此。为了节省其他人的时间,您还需要使用布局中可能有的任何空列表视图来执行此操作,例如“@android:id/empty”。事后看来,这是显而易见的,但我甚至没有想过。不过,这并不能解决应用程序中定义的自定义名称空间的自定义视图,不是吗?我仍然看到这个问题。
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>support-v4</artifactId>
            <version>r13</version>
        </dependency>
    
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>support-v4</artifactId>
            <version>r7</version>
        </dependency>