5

我正在尝试在对话框中显示 GMap,可以吗?

我的布局为

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

并将此 XML 膨胀到一个对话框中,如下所示...

    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();

但是,在通货膨胀期间,我得到了运行时错误......

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

谁能帮我解决这个问题??

4

1 回答 1

5

当布局包含片段http://developer.android.com/about/versions/android-4.2.html#NestedFragments时,您不能将布局膨胀到片段中

MapFragment 应该在代码中动态添加,例如:

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

并在 xml 布局中:

<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>
于 2013-03-25T13:24:04.937 回答