简体   繁体   中英

Google Map API V2 Android Fragment

I'm working on a project and I need to include Google Map. I'm using the API v2 and I have some problems to display the map.

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

This is my Geoloc.java, and in my map.xml i have this :

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

I don't have any problem to compile or to launch my app, but when i click on my button, my app dosn't want to work. When i double click on the LogCat, the error seems to be there :

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

But I don't really understand why. Someone have a idea ?

EDIT :

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

EDIT EDIT :

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

Here is the problem ...

In that case you have to :

1) Extend your Class With Fragment

2) In your fragment xml Take Relative layout and put your fragment in it with id map

3) in Fragment class Add your Fragment xml

4) get map id in onActivityCreated() using below code

googleMap=((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

5)there are no fifth step..:) Thats it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM