【问题标题】:Error inflating class - NestedScrollView - class not found膨胀类时出错 - NestedScrollView - 找不到类
【发布时间】:2016-06-15 21:40:24
【问题描述】:

运行 Android Studio 2.1.2、Windows 7。

我用 NestedScrollView 替换了 ScrollView,现在我得到了

android.view.InflateException:二进制 XML 文件第 2 行:错误 膨胀类 NestedScrollView

java.lang.ClassNotFoundException: 找不到类 路径上的“android.view.NestedScrollView”: /data/app/com.assemblyguide.remote-48.apk

...当我对该 XML 文件调用 SetContentView() 时。当我只有一个 ScrollView 时,我没有得到它。

我已尝试清理、使缓存无效并重建。 XML 看起来像这样。 . .

<?xml version="1.0" encoding="utf-8"?>
<NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- This linear layout is because the scrollview can have only 1 direct child -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <!-- Relative layout for Workorder -->
        <RelativeLayout
            android:id="@+id/rellayWorkorder"
            android:background="#383838"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="2dp">
            <TextView
                android:id="@+id/workorderlabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_margin="2dp"
                android:gravity="left"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Work Order:"/>

            <TextView
                android:id="@+id/workorderContent"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_margin="2dp"
                android:gravity="right"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="---workorder---"/>
        </RelativeLayout>


        <!-- Relative layout for Required Time
   <FrameLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_gravity="center_horizontal"></FrameLayout> -->

        <RelativeLayout
            android:id="@+id/rellayRequiredTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="2dp">
            <TextView
                android:id="@+id/requiredTimelabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_margin="2dp"
                android:gravity="left"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Required Time:"/>

            <TextView
                android:id="@+id/requiredTimeContent"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_margin="2dp"
                android:gravity="right"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="--- 00 minutes ---"/>
        </RelativeLayout>

        <!-- Relative layout for Time remaining -->
        <RelativeLayout
            android:id="@+id/rellayTimeRemaining"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="2dp">
            <TextView
                android:id="@+id/timeremaininglabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_margin="2dp"
                android:gravity="left"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Time Remaining:"/>

            <TextView
                android:id="@+id/tviewtimeremainingContent"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_margin="2dp"
                android:gravity="right"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="--- 0:00:00---"/>
        </RelativeLayout>

        <!--  Linear layout for Record Start / Record End buttons -->
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"  >
            <Button
                android:id="@+id/debulkrecordStart"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:layout_marginRight="4dp"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:onClick="OnSetRecordStartTimeClick"
                android:text="Record Start"/>
            <Button
                android:id="@+id/debulkrecordEnd"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:layout_marginRight="4dp"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:onClick="OnSetRecordEndTimeClick"
                android:text="Record End"/>
        </LinearLayout>

        <!-- Relative layout for Vacuum level -->
        <RelativeLayout
            android:id="@+id/rellayvacuumlevel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp">
            <TextView
                android:id="@+id/vaclabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_margin="2dp"
                android:gravity="left"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Vacuum Level (inches Hg):"/>
            <EditText
                android:id="@+id/vacleveledit"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight = "true"
                android:layout_margin="2dp"
                android:gravity="left"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:inputType="text|textCapCharacters"
                android:text="vac level"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_marginBottom="2dp"/>
        </RelativeLayout>

        <!-- Relative layout for Vac Gauge Equipment # -->
        <RelativeLayout
            android:id="@+id/rlayvacuumGauge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp">
            <TextView
                android:id="@+id/vacgaugelabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_margin="2dp"
                android:gravity="left"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Vac Gauge Equipment #:"/>
            <EditText
                android:id="@+id/vacgaugeedit"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight = "true"
                android:layout_margin="2dp"
                android:gravity="left"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:inputType="text|textCapCharacters"
                android:text="equip. #"/>
        </RelativeLayout>



        <!-- Relative layout for Calibration Due date -->
        <RelativeLayout
            android:id="@+id/rlaycalibdue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp">
            <TextView
                android:id="@+id/calibduelabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_margin="2dp"
                android:gravity="left"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Calibration Due Date:"/>
            <EditText
                android:id="@+id/calibdueedit"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight = "true"
                android:layout_margin="2dp"
                android:gravity="left"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:inputType="text|textCapCharacters"
                android:text="mm/dd/yyyy"/>
        </RelativeLayout>


        <CalendarView
            android:id="@+id/debulkcalendar"
            android:layout_width="300dp"
            android:layout_height="250dp"
            android:minDate="01/01/2016"
            android:maxDate="11/30/2016"
            />


        <!--  this linear layout is for the debulk override and done buttons  -->
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"  >
            <Button
                android:id="@+id/debulkOverride"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:layout_marginRight="4dp"
                android:onClick="OnResetClick"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Override"/>
            <Button
                android:id="@+id/debulkDone"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:layout_marginRight="2dp"
                android:onClick="onDoneBtnClick"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Done"/>
        </LinearLayout>

    </LinearLayout>

</NestedScrollView>

这个错误是什么意思,我该如何解决?

【问题讨论】:

    标签: android android-layout android-nestedscrollview


    【解决方案1】:

    类的全名是android.support.v4.widget.NestedScrollView。用&lt;android.support.v4.widget.NestedScrollView&gt; 替换&lt;NestedScrollView&gt; 元素,它应该可以正常工作。确保您的 build.gradle 文件中也有 v4 支持库。

    【讨论】:

    • 谢谢,但我还是不明白:为什么这在 构建时 没有在 XML 中标记错误?另外,什么是支持库,即支持库和普通的Android类有什么区别?
    • 支持库是后来添加的某些 android 功能的向后移植。例如,Toolbar 小部件是在 v21 中添加的。这意味着如果您想在您的应用程序中使用工具栏,您的 minSdk 需要为 21。android.support.v7.widget.Toolbar 的实现允许我们使用 minSdk 为 7 的工具栏。关于:构建时间 xml 错误,我不确定,但如果你使用的是 Android Studio,你可以点击“设计”选项卡,它会显示错误。
    • 在最新的 android x 中,这个解决方案成功了。试试“androidx.core.widget.NestedScrollView”
    【解决方案2】:

    如果使用 androidx,请将 &lt;NestedScrollView&gt; 或完整的类名 &lt;android.support.v4.widget.NestedScrollView&gt;替换为
    &lt;androidx.core.widget.NestedScrollView&gt;

    【讨论】:

    • 我正在使用 androidx,我正在使用 androidx.core.widget.NestedScrollView,但由于某种原因,我仍然收到通货膨胀错误。
    【解决方案3】:

    改成

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    

    【讨论】:

      【解决方案4】:

      需要在依赖项中添加这一行:

      compile 'com.android.support:support-v4:23.4.0'
      

      正如 chessdork 所说,它是 android.support.v4.widget.NestedScrollView 而不是 NestedScrollView

      【讨论】:

      • 你所说的“依赖”是什么意思?
      • 如果你使用的是android studio,你会在build.gradle文件中找到它
      【解决方案5】:

      当我使用 androidx.core.widget.NestedScrollView 时,我删除了这一行,它会正常工作。当我添加这个时,应用程序会崩溃(我的布局在 NestedScrollView 中也有 recyclerview)

      android:nestedScrollingEnabled="false"
      

      【讨论】:

      • 获取与此选项相关的类似崩溃。所以我知道如果你想让 RecyclerView 不可滚动,你应该为 RecyclerView 添加这一行,而不是 NestedScrollView。
      【解决方案6】:

      NestedScrollView 被添加到version 22.1.0
      您应该按以下方式使用 NestedScrollView:

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
      </android.support.v4.widget.NestedScrollView>
      

      build.gradle(模块)

      dependencies {
          implementation 'com.android.support:appcompat-v7:<greater than or equal to 22.1.0>'
          //...
      }
      

      【讨论】:

        【解决方案7】:

        一个愚蠢的错误可能是

        android:nestedScrollingEnabled="false"
        

        在 NestedScrollView 本身上设置。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-01-22
          • 1970-01-01
          • 2019-11-29
          • 2017-02-28
          • 2021-07-10
          • 2020-08-01
          • 2016-02-28
          • 2013-05-17
          相关资源
          最近更新 更多