java - ClassNotFoundException : android. View .include

标签 java android unit-testing gradle

我正在编写一个 Android 单元测试,测试一个 Fragment 是否启动了一个包含另一个 Fragment 的 Activity。 Activity 布局看起来像

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <fragment
        android:id="@+id/signup_finish_fragment"
        class="com.example.MyFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

当我启动测试时,我在 inflater.inflate() 方法中得到以下异常:

java.lang.ClassNotFoundException: android.view.include
  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
  at android.view.LayoutInflater.createView(LayoutInflater.java:552)
  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:636)
  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
  at com.example.MyFragment.onCreateView(MyFragment.java:77)

这个异常的原因是什么,我该如何解决这个问题?我在官方文档中没有发现任何关于 android.view.include 类的提及。

UPD:这是 MyFragment 布局代码:

<?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:orientation="vertical" >

    <EditText
        android:id="@id/first_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@id/last_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" />
    <LinearLayout
        android:id="@id/captcha_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <RelativeLayout 
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent">
                <ImageView
                    android:id="@id/captcha"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"/>
                <ProgressBar 
                    android:id="@id/captcha_progress"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:indeterminate="true"
                    android:visibility="gone"/>
            </RelativeLayout>
            <ImageButton 
                android:id="@id/refresh_captcha"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/btn_signup_refresh"
                android:scaleType="center"/>
        </LinearLayout>
        <EditText
            android:id="@id/captcha_code"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox 
            android:id="@id/agreement_checkbox"        
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/btn_check"/>
        <TextView
            android:id="@id/agreement_link"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical|left"/>
    </LinearLayout>
    <Button
        android:id="@id/done"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/done" />

</LinearLayout>

最佳答案

检查是否在 java Build Pathorder and export 部分检查了私有(private)库,清除并重新构建

关于java - ClassNotFoundException : android. View .include,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19450523/

相关文章:

Java 8 Base64 JWT token 转换为 JSON

android - 如何将 ZIP 文件导入 Android Studio?

javascript - Jest : Mock ES6 Module with both default and named export

android - 在 Cordova 和 Ionic 中检测屏幕关闭/打开

android - 将 Playstore 添加到 Android Studio 模拟器

angularjs - 如何使用 jasmine 测试 John papa vm.model 单元测试?

c# - 无法设置 TestContext 属性

java - 为什么这个 AES 实现会解包并再次包装 key ?

java - 使用 Google Guava 库进行 Android 开发是个好主意吗?

java - Powershell 输出格式错误的文件名