ClassNotFoundException for android.view.Space when running application
Asked Answered
S

4

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.

Strategic answered 26/11, 2011 at 19:31 Comment(2)
Do you use API 14? SDK 14 is required by space layoutBurkholder
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!Strategic
B
13

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.

Batho answered 26/11, 2011 at 19:45 Comment(4)
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.Strategic
As Style said, Space was introduced in Ice Cream Sandwich. View has been there since the being of (Android) time :)Batho
Space class is available in the support library v4 as a public class, developer.android.com/reference/android/support/v7/widget/…Abode
also repleace using androix lib, Try androidx.legacy.widget.SpaceProclamation
S
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 …>
Stratification answered 6/5, 2022 at 20:59 Comment(0)
B
13

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.

Batho answered 26/11, 2011 at 19:45 Comment(4)
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.Strategic
As Style said, Space was introduced in Ice Cream Sandwich. View has been there since the being of (Android) time :)Batho
Space class is available in the support library v4 as a public class, developer.android.com/reference/android/support/v7/widget/…Abode
also repleace using androix lib, Try androidx.legacy.widget.SpaceProclamation
N
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
Noellenoellyn answered 29/7, 2013 at 8:14 Comment(2)
what is min API for android.support.v7.widget.Space, not working on API10Meuser
Space class is now working under android.support.v4 package, use this reference android.support.v4.SpaceAbode
R
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

Rosemonde answered 28/2, 2017 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.