Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Maps API v2 in Dialog

I am trying to display GMaps in a Dialog, can it be done?

I have the layout as

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/LoginFormContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp" >

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    class="com.google.android.gms.maps.MapFragment"
    android:gravity="center" />

</RelativeLayout>

and im inflating this XML to a dialog, as below...

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.dialog_location, null);

    AlertDialog.Builder builder = new AlertDialog.Builder(context).setView(layout);
    AlertDialog alertDialog = builder.create();
    alertDialog.show();

But, during inflation, i get Runtime errors...

02-02 12:52:56.045: W/dalvikvm(23044): threadid=1: thread exiting with uncaught exception (group=0x40f862a0)
02-02 12:52:56.050: E/AndroidRuntime(23044): FATAL EXCEPTION: main
02-02 12:52:56.050: E/AndroidRuntime(23044): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at com.amithgc.taskflow.handler.activationMethods.dialog.ActivationLocationDialog.show(ActivationLocationDialog.java:31)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at com.amithgc.taskflow.handler.activationMethods.ActivationMethodLocation.showLinkingPopup(ActivationMethodLocation.java:37)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at com.amithgc.taskflow.profile.ActivationMethodItemView$1.onClick(ActivationMethodItemView.java:62)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.view.View.performClick(View.java:4223)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.view.View$PerformClick.run(View.java:17275)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.os.Handler.handleCallback(Handler.java:615)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.os.Looper.loop(Looper.java:137)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at java.lang.reflect.Method.invoke(Method.java:511)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at dalvik.system.NativeStart.main(Native Method)
02-02 12:52:56.050: E/AndroidRuntime(23044): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.app.Fragment.instantiate(Fragment.java:584)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.app.Fragment.instantiate(Fragment.java:552)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.app.Activity.onCreateView(Activity.java:4834)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
02-02 12:52:56.050: E/AndroidRuntime(23044):    ... 18 more
02-02 12:52:56.050: E/AndroidRuntime(23044): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
02-02 12:52:56.050: E/AndroidRuntime(23044):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-02 12:52:56.050: E/AndroidRuntime(23044):    at android.app.Fragment.instantiate(Fragment.java:574)
02-02 12:52:56.050: E/AndroidRuntime(23044):    ... 21 more

Can anyone please help me with this??

like image 626
amithgc Avatar asked Feb 02 '13 07:02

amithgc


1 Answers

You cannot inflate a layout into a fragment when that layout includes a fragment http://developer.android.com/about/versions/android-4.2.html#NestedFragments

MapFragment should be added dynamically in code, for example:

MapFragment fragment = new MapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.mapView, fragment).commit();

and in xml layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 style="@style/LoginFormContainer"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:padding="0dp" >

<FrameLayout
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

</RelativeLayout>
like image 128
Vladimir Avatar answered Sep 27 '22 17:09

Vladimir