【问题标题】:ClassNotFoundException for android.view.Space when running application运行应用程序时 android.view.Space 的 ClassNotFoundException
【发布时间】:2011-11-26 19:31:20
【问题描述】:

当我的活动布局中存在某些元素时,我在运行我的应用程序时遇到问题。我有以下布局,当我包含“空格”元素时出现问题:

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

<Button
       android:id="@+id/button1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_alignParentLeft="true"
       android:text="@string/foursquare" />

<Button
      android:id="@+id/button2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_above="@+id/foursquare_button"
      android:layout_alignParentLeft="true"
      android:text="@string/yelp" />

<Space
    android:layout_width="match_parent"
    android:layout_height="100px"
    android:layout_weight="0.18" />
</LinearLayout>

我得到的错误是这样的:

11-26 11:14:09.875:E/AndroidRuntime(10485):致命异常:main
...
11-26 11:14:09.875: E/AndroidRuntime(10485): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.infoit.nfc.activity/com.infoit.nfc.activity.ViewTag}: android.view .InflateException:二进制 XML 文件第 23 行:膨胀类空间时出错
...
11-26 11:14:09.875:E/AndroidRuntime(10485):原因:android.view.InflateException:二进制 XML 文件第 23 行:膨胀类空间时出错
...
11-26 11:14:09.875: E/AndroidRuntime(10485): 由: java.lang.ClassNotFoundException: android.view.Space in loader dalvik.system.PathClassLoader[/data/app/com.infoit.nfc.activity -2.apk]
...

如果我删除 Space 元素,一切都会变得非常敏锐。即使我认为定义 xmlns 可以解决问题,它也无法找到 Space 类。我觉得这很简单,但我想念它。

【问题讨论】:

  • 您使用 API 14 吗?空间布局需要 SDK 14
  • 啊,有道理。我在 API 14 上进行开发,但将其部署到 Gingerbread 手机上。因此,如果我使用的是合适的冰淇淋三明治手机,这可能不会给出错误。谢谢!

标签: android android-layout classnotfoundexception


【解决方案1】:

xml 文件需要引用由平台或您自己的项目定义的现有小部件,Space 不是标准的 Android 小部件。尝试用View 替换它。

【讨论】:

  • 谢谢,成功了。对于我自己的启迪,什么描述了标准的 Android 小部件?我通过从布局组件调色板中拖放获得了这个 Space 元素,我认为任何东西都会是标准的。
  • 正如 Style 所说,Space 是在冰淇淋三明治中引入的。 View 自(Android)时代以来一直存在 :)
  • Space 类在支持库 v4 中作为公共类提供,developer.android.com/reference/android/support/v7/widget/…
【解决方案2】:

空间是在 API 14 中引入的,但它也可以从 android support v7 中获得:

    <android.support.v7.widget.Space
            android:layout_width="match_parent"
            android:layout_height="12dp"/>

顺便说一句:

  • 使用dp 而不是px
  • 无需在垂直LinearLayout 中指定android:layout_height 的权重

【讨论】:

  • android.support.v7.widget.Space 的最小 API 是什么,不适用于 API10
  • Space 类现在在android.support.v4 包下工作,使用此参考android.support.v4.Space
【解决方案3】:

其他答案对我不起作用。最后,我将其更改为 v4,如下所示:

android.support.v4.widget.Space

效果很好。

类参考:https://developer.android.com/reference/android/support/v4/widget/Space.html

【讨论】:

    猜你喜欢
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 2015-06-09
    • 2012-11-28
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多