10

I'm having an issue running my application when certain elements exist in the layout of my activity. I have the following layout, and I have issue when I include the "Space" element:

<?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>

The error I get is this:

11-26 11:14:09.875: E/AndroidRuntime(10485): FATAL EXCEPTION: main
...
11-26 11:14:09.875: E/AndroidRuntime(10485): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.infoit.nfc.activity/com.infoit.nfc.activity.ViewTag}: android.view.InflateException: Binary XML file line #23: Error inflating class Space
...
11-26 11:14:09.875: E/AndroidRuntime(10485): Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class Space
...
11-26 11:14:09.875: E/AndroidRuntime(10485): Caused by: java.lang.ClassNotFoundException: android.view.Space in loader dalvik.system.PathClassLoader[/data/app/com.infoit.nfc.activity-2.apk]
...

If I remove the Space element everything is peachy keen. Somehow it's not able to find the Space class even though I thought defining the xmlns would solve the issue. I feel this is something simple, but I am missing it.

Christian
  • 799
  • 7
  • 15
  • 1
    Do you use API 14? SDK 14 is required by space layout – Vladimir Nov 26 '11 at 19:45
  • Ah, that makes sense. I was developing on API 14, but deployed it to a Gingerbread phone. Hence, this probably wouldn't give the error if I were on a proper Ice Cream Sandwich phone. Thanks! – Christian Nov 26 '11 at 22:40

4 Answers4

14

Using the View component in place of Space might work.

But I would try to keep Space, but using the following:

<android.widget.Space …>

It tends to work more reliably than when it worked with <Space …>.

Another option is using the legacy version:

<androidx.legacy.widget.Space …>
Thiengo
  • 226
  • 1
  • 3
  • 33
11

The xml file needs to refer to existing widgets either defined by the platform or by your own project, and Space is not a standard Android widget. Try replacing it with View instead.

chiuki
  • 14,580
  • 4
  • 40
  • 38
  • Thanks, that did the trick. For my own edification, what describes a standard Android widget? I got this Space element by dragging and dropping from the layout component palette, I would have thought anything there would be standard. – Christian Nov 26 '11 at 20:15
  • 2
    As Style said, `Space` was introduced in Ice Cream Sandwich. `View` has been there since the being of (Android) time :) – chiuki Nov 26 '11 at 23:22
  • Space class is available in the support library v4 as a public class, https://developer.android.com/reference/android/support/v7/widget/Space.html – saulmm Mar 27 '17 at 10:28
  • also repleace using androix lib, Try androidx.legacy.widget.Space – Lokesh Nov 07 '22 at 07:53
7

Space was introduced in API 14 but it's also available from android support v7:

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

By the way:

  • use dp instead of px
  • no need of secify the android:layout_height in a vertical LinearLayout with weight
Romain Piel
  • 11,017
  • 15
  • 71
  • 106
2

The other answers didn't work for me. Finally, I changed it to v4 like this:

android.support.v4.widget.Space

and it worked fine.

Class reference: https://developer.android.com/reference/android/support/v4/widget/Space.html

Pang
  • 9,564
  • 146
  • 81
  • 122
JaviMar
  • 375
  • 5
  • 18