【问题标题】:Error inflating class ImageView膨胀类 ImageView 时出错
【发布时间】:2013-05-17 21:23:24
【问题描述】:

我间歇性地收到 InflateException/ClassNotFoundException 错误。我以前在 SO 中看到过类似的错误,但它们是由拼写错误引起的。我正确拼写了“ImageView”,所以我不知道是什么导致了错误。

发生错误的代码是:

v = View.inflate(getContext(), R.layout.event_show_row_layout, null);

这是布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="10dip"
        android:paddingRight="10dip" >

        <TextView
            android:id="@+id/fromTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:textStyle="italic" />

        <TextView
            android:id="@+id/timeTextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:textSize="12sp"
            android:textStyle="italic" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layoutPostImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="4dp" >

        <ImageView
            android:id="@+id/postImageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_margin="2dp"
            android:adjustViewBounds="true"
            android:background="@drawable/timeline_image_border"
            android:contentDescription="@string/hello"
            android:paddingBottom="6dp"
            android:scaleType="fitXY"
            android:src="@drawable/timeline_image_dummy" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/wordsRelativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/wordsTextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical|center_horizontal"
            android:paddingLeft="50dp"
            android:paddingRight="50dp"
            android:text="@string/hello" />

        <ImageView
            android:id="@+id/topLeftQuoteImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:adjustViewBounds="true"
            android:contentDescription="@string/hello"
            android:maxHeight="25dp"
            android:maxWidth="25dp"
            android:scaleType="fitXY"
            android:src="@drawable/po_quotes_mobile1" />

        <ImageView
            android:id="@+id/bottomRightQuoteImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="12dp"
            android:layout_marginRight="12dp"
            android:adjustViewBounds="true"
            android:contentDescription="@string/hello"
            android:maxHeight="25dp"
            android:maxWidth="25dp"
            android:scaleType="fitXY"
            android:src="@drawable/po_quotes_mobile2" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp" >

        <Button
            android:id="@+id/commentButton"
            android:layout_width="84dp"
            android:layout_height="24dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dp"
            android:background="@drawable/es_comment_btn" />

        <Button
            android:id="@+id/buttonLike"
            android:layout_width="84dp"
            android:layout_height="24dp"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/commentButton"
            android:background="@drawable/es_like_btn" />

        <Button
            android:id="@+id/buttonNumberLike"
            android:layout_width="wrap_content"
            android:layout_height="24dp"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/buttonLike"
            android:background="@drawable/like_counter"
            android:textColor="@android:color/white"
            android:textSize="12sp" />

        <Button
            android:id="@+id/buttonDelete"
            android:layout_width="38dp"
            android:layout_height="24dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp"
            android:background="@drawable/es_more_btn" />
    </RelativeLayout>

    <TextView
        android:id="@+id/commentsTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:background="#FFF9F8F1"
        android:text="@string/hello"
        android:textColor="@android:color/black"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/spacerTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp" />

</LinearLayout>

这是错误日志:

E/AndroidRuntime(15849): FATAL EXCEPTION: main
E/AndroidRuntime(15849): android.view.InflateException: Binary XML file line #36: Error inflating class ImageView
E/AndroidRuntime(15849):    at org.holoeverywhere.LayoutInflater.createViewFromTag(LayoutInflater.java:454)
E/AndroidRuntime(15849):    at org.holoeverywhere.LayoutInflater.rInflate(LayoutInflater.java:737)
E/AndroidRuntime(15849):    at  org.holoeverywhere.LayoutInflater.rInflate(LayoutInflater.java:740)
E/AndroidRuntime(15849):    at org.holoeverywhere.LayoutInflater.inflate(LayoutInflater.java:542)
E/AndroidRuntime(15849):    at org.holoeverywhere.LayoutInflater.inflate(LayoutInflater.java:492)
E/AndroidRuntime(15849):    at org.holoeverywhere.LayoutInflater.inflate(LayoutInflater.java:487)
E/AndroidRuntime(15849):    at android.view.View.inflate(View.java:8807)
E/AndroidRuntime(15849):    at com.mypackage.adapters.PostAdapter.getPostView(PostAdapter.java:283)
E/AndroidRuntime(15849):    at com.mypackage.adapters.PostAdapter.getView(PostAdapter.java:268)
E/AndroidRuntime(15849):    at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
E/AndroidRuntime(15849):    at android.widget.AbsListView.obtainView(AbsListView.java:1430)
E/AndroidRuntime(15849):    at android.widget.ListView.makeAndAddView(ListView.java:1745)
E/AndroidRuntime(15849):    at android.widget.ListView.fillDown(ListView.java:670)
E/AndroidRuntime(15849):    at android.widget.ListView.fillFromTop(ListView.java:727)
E/AndroidRuntime(15849):    at android.widget.ListView.layoutChildren(ListView.java:1598)
E/AndroidRuntime(15849):    at android.widget.AbsListView.onLayout(AbsListView.java:1260)
E/AndroidRuntime(15849):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(15849):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
E/AndroidRuntime(15849):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(15849):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
E/AndroidRuntime(15849):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
E/AndroidRuntime(15849):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
E/AndroidRuntime(15849):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(15849):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
E/AndroidRuntime(15849):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(15849):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
E/AndroidRuntime(15849):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(15849):    at net.simonvt.menudrawer.LeftDrawer.onLayout(LeftDrawer.java:64)
E/AndroidRuntime(15849):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(15849):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
E/AndroidRuntime(15849):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(15849):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
E/AndroidRuntime(15849):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
E/AndroidRuntime(15849):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
E/AndroidRuntime(15849):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(15849):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
E/AndroidRuntime(15849):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(15849):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
E/AndroidRuntime(15849):    at android.view.View.layout(View.java:7175)
E/AndroidRuntime(15849):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
E/AndroidRuntime(15849):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
E/AndroidRuntime(15849):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(15849):    at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(15849):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(15849):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(15849):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(15849):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(15849):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(15849):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(15849): Caused by: java.lang.ClassNotFoundException: Could not find class: ImageView
E/AndroidRuntime(15849):    at org.holoeverywhere.LayoutInflater.onCreateView(LayoutInflater.java:607)
E/AndroidRuntime(15849):    at org.holoeverywhere.LayoutInflater.createViewFromTag(LayoutInflater.java:448)
E/AndroidRuntime(15849):    ... 48 more

【问题讨论】:

  • 请添加event_show_row_layout layout xml 以获取更多帮助
  • 刚刚添加了布局xml。谢谢!
  • 这个问题中描述的问题看起来很相似。 stackoverflow.com/questions/15189957/…
  • 在设备上试用。很多时候是模拟器的问题。
  • 膨胀过程中的错误可能来自ImageView中使用的drawable,拼错了此imageview中使用的png文件的名称,因此无法找到该文件。 “找不到文件”的愚蠢和严重错误。叹息,我真的很讨厌 Android 的复杂性。

标签: android inflate-exception


【解决方案1】:

我遇到了同样的问题。 我的问题发生了,因为我刚刚在“drawable-v24”文件夹中使用了图像(图标)。 我通过将它们复制到“可绘制”文件夹来解决它

【讨论】:

    【解决方案2】:

    我最近遇到了同样的问题,并且能够使用 app:srcCompat="@drawable/ic_black_image_24" 解决它

    appcompat:srcCompat的文档指出 -

    它将一个drawable设置为这个ImageView的内容。在旧版本平台上运行时允许使用矢量可绘制对象。

    如果有人偶然发现同样的问题,希望这会有所帮助。

    【讨论】:

    • 非常感谢,这是我的罪魁祸首
    【解决方案3】:

    对于我来说,我将图像复制到文件夹 drawable-v24 并且编译器没有找到它

    将Android Studio的视图更改为项目视图

    并将您的图像剪切并粘贴到文件夹drawable

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,因为我在 Android 视图上将我的 png 文件复制到 drawables,它会自动复制到 drawable-v24,但是您需要将文件复制到 drawable,您可以导航到您的项目,然后重新/drawables 并手动粘贴它,或将视图更改为在可绘制文件夹中过去的投影视图

      【讨论】:

        【解决方案5】:

        我自己也有这个问题,因为我的图像在 darawable-v24 目录中。所以当我想使用旧的 api 时,应用程序崩溃了。所以我建议检查你的资源是否在正确的目录中。

        https://stacklearn.ir

        【讨论】:

          【解决方案6】:

          尝试改变

          v = View.inflate(getContext(), R.layout.event_show_row_layout, null);
          

          v = View.inflate(getApplicationContext(), R.layout.event_show_row_layout, null);
          

          【讨论】:

            【解决方案7】:

            这是一个老问题,但我刚刚遇到了同样的问题。在我的情况下,这是由于使用带有selector 而不是colorandroid:tint 属性引起的:

            <ImageView
                ...
                android:tint="@drawable/some_selector" />
            

            虽然这在 SDK 21+ 上没有问题,但它在旧版本中崩溃...

            切换到AppCompatImageViewapp:tint 解决了这个问题:

            <android.support.v7.widget.AppCompatImageView
                ...
                app:tint="@drawable/some_selector" />
            

            【讨论】:

              【解决方案8】:

              对我来说,在我的一个 layout.xml 文件中,我有

              <ImageView
                  android:id="@+id/row_1_col_0"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@string/default_picture_location">
              </ImageView>
              

              在strings.xml里面,我有

              <string name="default_picture_location">"@mipmap/tile"</string>
              

              所以它在 Android Studio 中显示了这个:

              <ImageView
                  android:id="@+id/row_1_col_0"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@mipmap/tile">
              </ImageView>
              

              我认为一切都会正常运行,因为没有错误并且应用程序已编译并运行。但是,有一个运行时错误说“android.view.InflateException:二进制 XML 文件行 #7:错误膨胀类 ImageView”。

              一旦我从

              更改了 android:background
              android:background="@string/default_picture_location"
              

              android:background="@mipmap/tile"
              

              一切正常。

              【讨论】:

              • 这不起作用的原因是你在字符串标签之间有引号;它们不是必需的。
              【解决方案9】:

              我有drawable-zh-xxhdpi目录下的资源文件,但是drawable-xxhdpi目录下没有资源,出现这个错误。

              这应该是:

              res
                drawable-xxhdpi
                   aa.png
                drawable-zh-xxhdpi
                   aa.png
              

              【讨论】:

                【解决方案10】:

                将图像放入可绘制而不是“v-24”对我有用,我停止了崩溃。

                【讨论】:

                  【解决方案11】:

                  膨胀过程中的错误可能来自ImageView中使用的drawable,在这种情况下:

                  android:background="@drawable/timeline_image_border"
                  android:src="@drawable/timeline_image_dummy"
                  

                  要摆脱它,请尝试使用不同的可绘制对象替换所有现有图像。

                  【讨论】:

                  • 我拼错了此图像视图中使用的 png 文件的名称,因此无法找到该文件。 “找不到文件”的愚蠢和严重错误。唉,我真的很讨厌 Android 的复杂性。
                  【解决方案12】:

                  不要像这样将 XML 文件和图像文件命名为同名:

                  【讨论】:

                    【解决方案13】:

                    你还没有关闭 LinearLayout 标签

                    请使用以下代码对 layout.xml 进行膨胀

                    LayoutInflater inflater = getLayoutInflater();
                    final View v = inflater.inflate(R.layout.your_layout_id, null);
                    

                    【讨论】:

                    • 有时,由于编辑器行为或编辑器中的错误,它不会抛出任何错误,而是会在模拟器或设备上运行。但Android比编辑器更聪明,它会捕捉错误并且不会膨胀xml来创建布局。
                    【解决方案14】:

                    在我的情况下,我遇到了同样的错误,但在使用 API

                    这是由于在我导入的 SVG 中使用了“渐变”造成的。

                    错误没有出现在stacktrace中,但出现在导入的SVG文件的.xml中:

                    对于 API

                    如果您只想使用 SVG,您可以将此资源的使用限制为 API >= 24,并移除以下 API 的渐变。

                    【讨论】:

                      【解决方案15】:

                      对我来说,当我将图像粘贴到 drawabledrawable-v24 并添加一些代码 android:src="@drawable/battman" 时,它可以工作

                      【讨论】:

                        【解决方案16】:

                        对我来说,问题是 svg 文件太大了。

                        【讨论】:

                          【解决方案17】:

                          在我的情况下,svg 的字母有间隙,然后这导致了错误,我所做的是修改 illustrator 中的字母以消除间隙并且它起作用了。

                          【讨论】:

                            【解决方案18】:

                            在我的例子中,它是带有长路径的 SVG 图像。它显示错误:

                            原因:java.lang.IllegalArgumentException:R 不是有效动词。 路径的位置 2 发生故障:STRING_TOO_LARGE

                            看着long SVG Asset file error : R is not a valid verb. Failure occurred at position 2 of path: STRING_TOO_LARGE我把paths分成了几个32Kb以下的。

                            【讨论】:

                              猜你喜欢
                              • 1970-01-01
                              • 2018-03-04
                              • 2017-03-06
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 2019-11-29
                              • 2017-02-28
                              相关资源
                              最近更新 更多