Google Map API V2 Android Fragment

时间:2014-07-28 12:04:16

标签: android android-fragments google-maps-api-2

我正在处理一个项目,我需要包含Google Map。我使用的是API v2,我在显示地图时遇到了一些问题。

    package com.lan.me;

import com.google.android.gms.maps.GoogleMap;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Geoloc extends Fragment {

    GoogleMap map;
    View view;

    public static Fragment newInstance() {
        Geoloc mFrgment = new Geoloc();
        return mFrgment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.map, container, false);

        return view;
    }
}

这是我的Geoloc.java,在我的map.xml中我有这个:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />  

编译或启动我的应用程序时没有任何问题,但是当我点击我的按钮时,我的应用程序无法正常工作。当我双击LogCat时,错误似乎在那里:

view = inflater.inflate(R.layout.map,container,false);

但我真的不明白为什么。有人有想法吗?

编辑:

07-29 06:00:14.202: E/AndroidRuntime(1169): FATAL EXCEPTION: main
07-29 06:00:14.202: E/AndroidRuntime(1169): Process: com.lan.me, PID: 1169
07-29 06:00:14.202: E/AndroidRuntime(1169): android.view.InflateException: Binary XML file line #1: Error inflating class fragment
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at com.lan.me.Geoloc.onCreateView(Geoloc.java:24)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.app.Fragment.performCreateView(Fragment.java:1700)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.app.Activity.onCreateView(Activity.java:4805)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at com.lan.me.Geoloc.onCreateView(Geoloc.java:24)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.app.Fragment.performCreateView(Fragment.java:1700)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.app.BackStackRecord.run(BackStackRecord.java:684)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.os.Handler.handleCallback(Handler.java:733)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.os.Looper.loop(Looper.java:136)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at java.lang.reflect.Method.invokeNative(Native Method)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at java.lang.reflect.Method.invoke(Method.java:515)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at dalvik.system.NativeStart.main(Native Method)
07-29 06:00:14.202: E/AndroidRuntime(1169): Caused by: java.lang.IllegalArgumentException: Binary XML file line #1: Duplicate id 0x7f090027, tag null, or parent id 0x7f090004 with another fragment for com.google.android.gms.maps.MapFragment
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.app.Activity.onCreateView(Activity.java:4791)
07-29 06:00:14.202: E/AndroidRuntime(1169):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
07-29 06:00:14.202: E/AndroidRuntime(1169):     ... 26 more

编辑编辑:

http://uprapide.com/image/811589-properties_2

这是问题......

1 个答案:

答案 0 :(得分:0)

在这种情况下,您必须:

1)用片段扩展你的课程

2)在你的片段xml中采取相对布局并将你的片段放入id地图

3)在Fragment类中添加你的Fragment xml

4)使用下面的代码

在onActivityCreated()中获取map id
googleMap=((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

5)没有第五步...... :) 多数民众赞成