运行应用程序时android.view.Space的ClassNotFoundException

时间:2011-11-26 19:31:20

标签: android android-layout classnotfoundexception

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

<?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):致命异议:主要
    ...
    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:loader dalvik.system.PathClassLoader中的android.view.Space [/data/app/com.infoit.nfc.activity -2.apk]
    ...

  

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

3 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:5)

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

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

顺便说一下:

  • 使用dp代替px
  • 无需将android:layout_height隐藏在垂直LinearLayout重量

答案 2 :(得分:2)

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

android.support.v4.widget.Space

它工作正常。

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