4

J'essaie de convertir une activité google map pour travailler à l'intérieur d'un fragment qui sera appelé dans NavigationView. La carte montre mais quand je l'ai utilisé le code getMapAsync Google map à l'intérieur d'un autre fragment - Erreur lors de l'appel getMapAsync

mFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); 
mFragment.getMapAsync(this); 

J'ai reçu un message d'erreur

E/AndroidRuntime: FATAL EXCEPTION: main 
E/AndroidRuntime: java.lang.NullPointerException 
E/AndroidRuntime: at com.sample.samplemap.samplemapFragment.onCreate(samplemapFragment.java:32) 
E/AndroidRuntime: at android.support.v4.app.Fragment.performCreate(Fragment.java:1942) 
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040) 
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259) 
E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1624) 
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 
E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725) 
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92) 
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137) 
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5306) 
E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511) 
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 

samplemapFragment.java

package com.sample.samplemap; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 

public class samplemapFragment extends Fragment implements OnMapReadyCallback { 
    GoogleMap mGoogleMap; 
    SupportMapFragment mFragment; 

    public samplemapFragment() { 
     Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);   
     mFragment.getMapAsync(this); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     //Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_sample_map, container, false); 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 

     mGoogleMap = googleMap; 
     mGoogleMap.setMyLocationEnabled(true); 
    } 
} 

fragment_sample_map.xml

<RelativeLayout 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
tools:context="com.sample.samplemap.samplemapFragment" 
xmlns:tools="http://schemas.android.com/tools"> 
    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="384dp" 
    android:layout_height="300dp" android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_marginTop="44dp" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" /> 
</RelativeLayout> 
+0

écrivez ici votre fichier xml – Vyacheslav

+0

@Vyacheslav j'ai maintenant inclus le xml, s'il vous plaît revoir la question. Merci – Mellorine

Répondre

9

J'ai vérifié que le LifeCycle du fragment est: https://developer.xamarin.com/guides/android/platform_features/fragments/part_1_-_creating_a_fragment/Images/fragment_lifecycle.png

OnCreate appelleront d'abord, puis OnCreateView.

Comme je l'ai mis en œuvre le code pour vous qui fonctionne très bien ici avec MapView:

MapCheckingActivity.java

package com.ramesh.expandablelistview; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 

public class MapCheckingActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_map_checking); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportFragmentManager().beginTransaction() 
       .replace(R.id.content_frame, 
         new SampleMapFragment()).commit(); 
    } 

} 

SampleMapFragment.java

package com.ramesh.expandablelistview; 

/** 
* Created by Ramesh Kumar on 2/17/2016. 
*/ 


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

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapView; 
import com.google.android.gms.maps.MapsInitializer; 
import com.google.android.gms.maps.OnMapReadyCallback; 


public class SampleMapFragment extends Fragment implements 
     OnMapReadyCallback { 


    GoogleMap mGoogleMap; 
    MapView mapView; 

    View mView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     MapsInitializer.initialize(getContext()); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
//  Inflate the layout for this fragment 
     mView = inflater.inflate(R.layout.fragment_map, container, false); 
     return mView; 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     mapView = (MapView) mView.findViewById(R.id.map); 
     if (mapView != null) { 
      // Initialise the MapView 
      mapView.onCreate(null); 
      mapView.onResume(); 
      // Set the map ready callback to receive the GoogleMap object 
      mapView.getMapAsync(this); 
     } 

    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     MapsInitializer.initialize(getContext()); 

     mGoogleMap = googleMap; 
     mGoogleMap.setMyLocationEnabled(true); 


    } 

} 

activity_map_checking.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:fitsSystemWindows="true" 
    tools:context="com.ramesh.expandablelistview.MapCheckingActivity"> 

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" 
     android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar android:id="@+id/toolbar" 
      android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_map_checking" /> 

    <android.support.design.widget.FloatingActionButton android:id="@+id/fab" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

content_map_checking.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/activity_map_checking" 
    tools:context="com.ramesh.expandablelistview.MapCheckingActivity"> 


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

</RelativeLayout> 

fragment_map.xml:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.sample.samplemap.samplemapFragment"> 

    <!--<fragment xmlns:android="http://schemas.android.com/apk/res/android"--> 
    <!--xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="384dp"--> 
    <!--android:layout_height="300dp" android:id="@+id/map"--> 
    <!--android:name="com.google.android.gms.maps.SupportMapFragment"--> 
    <!--android:layout_marginTop="44dp"--> 
    <!--android:layout_alignParentTop="true" />--> 

    <com.google.android.gms.maps.MapView 
     android:id="@+id/map" android:layout_marginTop="44dp" 
     android:layout_width="384dp" android:layout_height="300dp"/> 
</RelativeLayout> 

enter image description here

J'ai créé avec carte dans le fragment par MapView.

En Menifest:

<meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="@string/google_maps_key" /> 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

Carte ne montre pas en raison de HashKey je suis entré mal Si elle aide à accepter puis comme réponse. Merci

+0

merci beaucoup, il a résolu mon problème – Mellorine

+0

juste un suivi, comment puis-je utiliser la fonction comme public void onConnected (Bundle bundle) {} et public void onLocationChanged (Location location) {} – Mellorine

+0

implémenter l'interface OnLocationChangeListener. –