InflateException on defining MapView in XML

920 views
Skip to first unread message

plusminus

unread,
Nov 18, 2007, 5:16:49 PM11/18/07
to Android Developers
I get a runtime exception (InflateException) having defined an MapView
in XML.
"Error inflating class MapView."
Using the following code:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is the "Main-Layout" a relative one -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/
res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- What comes within here, is a child of the outer layout -->
<!-- This is the Map all actions will take place -->
<MapView id="@+id/main_map"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:"/>

<Button id="@+id/zoomin_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/main_map"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:text="+" />
<Button id="@+id/zoomout_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeft="@id/zoomin_button"
android:layout_alignTop="@id/zoomin_button"
android:text="-" />
</RelativeLayout>

Also tried to simplify it, always the same exeption...
Someone got an explanaition or solution for that ?

Regards, plusminus
http://anddev.org | Android Development Community / Tutorials

daniel.benedykt

unread,
Nov 18, 2007, 5:31:16 PM11/18/07
to Android Developers
Hi plusminus

I had the same problem, so I decompiled the MapView Class and I found
the following code in the constructor:

public MapView(Context context)
{
super(context);
mDrawer = new AndroidGraphics(null);
mOverlayBundle = null;
mReticle = null;
if(context instanceof MapActivity)
((MapActivity)context).setupMapView(this);
else
throw new IllegalArgumentException("MapViews can only be
created inside instances of MapActivity.");
}

So for some reason is only allowed to create a MapView inside a
MapActivity.
I will try to investigate more.

Daniel
> Regards, plusminushttp://anddev.org | Android Development Community / Tutorials

plusminus

unread,
Nov 18, 2007, 6:37:11 PM11/18/07
to Android Developers
Hi, daniel

I've also had that during my various, tries. But this is probably not
the source for the problem, as another Exception is thrown...
Can someone explain the meaning of InflateException as I'm not a
native speaker, i do not really understand it.
I think of a balloon that is blown to big... o_O

Regards, plusminus
http://anddev.org | Android Development Community / Tutorials

On Nov 18, 11:31 pm, "daniel.benedykt" <daniel.bened...@gmail.com>
wrote:
Message has been deleted

Teo

unread,
Nov 20, 2007, 10:18:31 AM11/20/07
to Android Developers
Hi, i have this problem too, but i don't think it's specific to Maps,
here's my main.xml:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<com.google.android.myApp.MyView
id="@+id/MyViewId"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/pic" />

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>

</FrameLayout>

Theoretically this should work, since i got it (and simplified it)
from the LunarLander sample, but for some reason i get that inflating
error :/

On Nov 19, 1:37 am, plusminus <stoeps...@gmx.de> wrote:
> Hi, daniel
>
> I've also had that during my various, tries. But this is probably not
> the source for the problem, as another Exception is thrown...
> Can someone explain the meaning of InflateException as I'm not a
> native speaker, i do not really understand it.
> I think of a balloon that is blown to big... o_O
>

Farhan

unread,
Nov 20, 2007, 11:24:42 AM11/20/07
to Android Developers
I am facing the same error. Can I mix the MapView with other view
elements in a LinearLayout? I am trying to do this but Android cannot
inflate my MapView class. I am using xml layout to create my MapView.
Here are the codes:

The xml file:

Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText id="@+id/location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the location here"
android:selectAllOnFocus="true"
/>
<MapView id="@+id/mapv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>


The only code file:

package com.russoue.testandroid;

import android.os.Bundle;
import android.widget.EditText;

import com.google.android.maps.MapActivity;

public class TestAndroid extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
EditText locationField = (EditText)
findViewById(R.id.location);
locationField.setSelection(0,
locationField.getText().length());
}
}

On Nov 20, 9:18 am, Teo <teomina...@gmail.com> wrote:
> Hi, i have this problem too, but i don't think it's specific to Maps,
> here's my main.xml:
>
> <?xml version="1.0" encoding="utf-8"?>
>
> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/
> android"
> android:layout_width="fill_parent"
> android:layout_height="fill_parent">
>
> <com.google.android.myApp.MyView
> id="@+id/MyViewId"
> android:layout_width="fill_parent"
> android:layout_height="fill_parent"
> android:background="@drawable/pic" />
>
> <RelativeLayout
> android:layout_width="fill_parent"
> android:layout_height="fill_parent" >
> </RelativeLayout>
>
> </FrameLayout>
>
> Theoretically this should work, since i got it (and simplified it)
> from the LunarLander sample, but for some reason i get that inflating
> error :/
>
> On Nov 19, 1:37 am,plusminus<stoeps...@gmx.de> wrote:
>
> > Hi, daniel
>
> > I've also had that during my various, tries. But this is probably not
> > the source for the problem, as another Exception is thrown...
> > Can someone explain the meaning of InflateException as I'm not a
> > native speaker, i do not really understand it.
> > I think of a balloon that is blown to big... o_O
>
> > Regards,plusminushttp://anddev.org| Android Development Community / Tutorials
>
> > On Nov 18, 11:31 pm, "daniel.benedykt" <daniel.bened...@gmail.com>
> > wrote:
>
> > > Hiplusminus
>
> > > I had the same problem, so I decompiled theMapViewClass and I found
> > > the following code in the constructor:
>
> > > publicMapView(Context context)
> > > {
> > > super(context);
> > > mDrawer = new AndroidGraphics(null);
> > > mOverlayBundle = null;
> > > mReticle = null;
> > > if(context instanceof MapActivity)
> > > ((MapActivity)context).setupMapView(this);
> > > else
> > > throw new IllegalArgumentException("MapViews can only be
> > > created inside instances of MapActivity.");
> > > }
>
> > > So for some reason is only allowed to create aMapViewinside a
> > > MapActivity.
> > > I will try to investigate more.
>
> > > Daniel
>
> > > On Nov 18, 5:16 pm,plusminus<stoeps...@gmx.de> wrote:
>
> > > > I get a runtime exception (InflateException) having defined anMapView
> > > > in XML.
> > > > "Error inflating classMapView."
> > > > Using the following code:
> > > > <?xml version="1.0" encoding="utf-8"?>
> > > > <!-- This is the "Main-Layout" a relative one -->
> > > > <RelativeLayout xmlns:android="http://schemas.android.com/apk/
> > > > res/android"
> > > > android:layout_width="fill_parent"
> > > > android:layout_height="fill_parent">
> > > > <!-- What comes within here, is a child of the outer layout -->
> > > > <!-- This is the Map all actions will take place -->
> > > > <MapViewid="@+id/main_map"
> > > > android:layout_width="fill_parent"
> > > > android:layout_height="wrap_content"
> > > > android:text="Type here:"/>
>
> > > > <Button id="@+id/zoomin_button"
> > > > android:layout_width="wrap_content"
> > > > android:layout_height="wrap_content"
> > > > android:layout_below="@id/main_map"
> > > > android:layout_alignParentRight="true"
> > > > android:layout_marginLeft="10px"
> > > > android:text="+" />
> > > > <Button id="@+id/zoomout_button"
> > > > android:layout_width="wrap_content"
> > > > android:layout_height="wrap_content"
> > > > android:layout_toLeft="@id/zoomin_button"
> > > > android:layout_alignTop="@id/zoomin_button"
> > > > android:text="-" />
> > > > </RelativeLayout>
>
> > > > Also tried to simplify it, always the same exeption...
> > > > Someone got an explanaition or solution for that ?
>
> > > > Regards,plusminushttp://anddev.org|Android Development Community / Tutorials

Teo

unread,
Nov 20, 2007, 11:27:46 AM11/20/07
to android-d...@googlegroups.com
Hi everyone,

i got around the error, not exactly sure how, but i think the problem was with the constructor of the class in the xml (and the super). It's now like this, and it works:

public MyView(Context context, AttributeSet attrs, Map inflateParams) {
        super(context, attrs, inflateParams);
//...
    }


Thanks,
Teo.

oscarmart...@gmail.com

unread,
Nov 26, 2007, 3:40:21 PM11/26/07
to Android Developers
You can try by replacing the View tag by this:

<view class="com.google.android.maps.MapView" id="@+id/gmv_mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

in the xml file.

It worked for me.

On Nov 20, 5:24 pm, Farhan <russ...@gmail.com> wrote:
> I am facing the same error. Can I mix theMapViewwith other view
> elements in a LinearLayout? I am trying to do this but Android cannot
> inflate myMapViewclass. I am using xml layout to create myMapView.
> Here are the codes:
>
> The xml file:
>
> Code:
>
> <?xml version="1.0" encoding="utf-8"?>
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> android"
> android:orientation="vertical"
> android:layout_width="fill_parent"
> android:layout_height="fill_parent"
> >
> <EditText id="@+id/location"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:text="Enter the location here"
> android:selectAllOnFocus="true"
> />
> <MapViewid="@+id/mapv"

LancelotSPA

unread,
Nov 26, 2007, 5:31:02 PM11/26/07
to Android Developers
Hey:

I´m having some problems with the layout. It works fine for me, but
what happen if I don´t want to use the original MapView, instead of
that I´m using myMapView that extends MapView. How can I put it in the
xml of the layout, I´ve tried but I got an error.. I also tried to use
the MapView and then make a cast to myMapView but I also got an error
on the emulator.

Thanks.

Mike Lyons

unread,
Nov 26, 2007, 6:40:45 PM11/26/07
to Android Developers
See http://code.google.com/android/reference/com/google/android/maps/MapView.html

On Nov 18, 5:31 pm, "daniel.benedykt" <daniel.bened...@gmail.com>
wrote:
...
> So for some reason is only allowed to create a MapView inside a
> MapActivity.
...
Reply all
Reply to author
Forward
0 new messages