3

我正在構建與Google地圖相關的應用程序。在這我試圖實現片段中的地圖。我在OnMapReady方法中操作地圖。但是app不調用OnMapReady方法。我在活動中實施了同樣的事情,並且其工作非常完美。但它不能在碎片中工作。我不知道它是否存在片段生命週期的問題,我錯過了一些東西。 這是代碼。onMapReady不會在片段中調用

Maps_Fragment.java

package com.example.mudasir.login; 


import android.content.pm.PackageManager; 
import android.location.Address; 
import android.location.Geocoder; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 

import com.google.android.gms.maps.CameraUpdate; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.LatLngBounds; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 

import java.io.IOException; 
import java.util.List; 


/** 
* A simple {@link Fragment} subclass. 
*/ 
public class Maps_Fragment extends Fragment implements OnMapReadyCallback { 

    public GoogleMap mMap; 
    String[] showroom_addresses = new String[5]; 
    List<Address> addressList = null; 
    LatLng latlng; 
    LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
    View view1; 

    public Maps_Fragment() { 
     // Required empty public constructor 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_maps_, container, false); 
    } 
    @Override 
    public void onViewCreated(View view,Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.maps); 
     mapFragment.getMapAsync(this); 
    } 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 

     showroom_addresses[0] = "Dubai"; 
     showroom_addresses[1] = "Fujairah"; 
     showroom_addresses[2] = "Al Ain"; 
     showroom_addresses[3] = "Sharjah"; 
     showroom_addresses[4] = "Ras Al Khaimah"; 
     Geocoder geocoder = new Geocoder(getActivity()); 
     for(int i =0; i<5; i++) { 
      try { 
       addressList = geocoder.getFromLocationName(showroom_addresses[i], 1); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      android.location.Address address = addressList.get(0); 
      latlng = new LatLng(address.getLatitude(), address.getLongitude()); 
      Marker marker = googleMap.addMarker(new MarkerOptions().position(latlng).title(showroom_addresses[i])); 
      builder.include(marker.getPosition()); 
     } 

     LatLngBounds bounds = builder.build(); 
     int padding = 0; 
     CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,padding); 
     //CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(new MarkerOptions().position(latlng).title(showroom_addresses[3]).getPosition(),7F); 
     googleMap.animateCamera(cu); 
    } 

} 

Maps_Fragment.xml

<FrameLayout 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.example.mudasir.login.Maps_Fragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/linearLayout"> 


     <fragment android:id="@+id/maps" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      class="com.google.android.gms.maps.MapFragment" 
      android:layout_alignParentTop="true"> 
      </fragment> 

相同的方法,並要求在活動,但它的一些錯誤的片段的情況下的情況下正常工作。請幫忙。

回答

1

您必須擴展從

SupportMapFragment 

,而不是

Fragment 

將帖子

我的建議是有2個片段:一,當你不有任何結果和另一個與SupportFragmentManager。

另外,在您的Maps_Fragment應該從SupportFragmentManager擴展,您必須刪除onCreateView和onViewCreated的覆蓋,否則您應該管理所有的SupportMapFragment,我認爲你不想。


             
  
    @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_maps_, container, false); } @Override public void onViewCreated(View view,Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.maps); mapFragment.getMapAsync(this); }] 
  

要實現OnMapReadyCallback,我會做它在onActivityCreated方法:

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    getMapAsync(this); 
} 
+0

現在的代碼工作,但它給mapFragment.getMapAsync(this)上的nulll異常;和應用程序崩潰。 –

+0

查看我的更新回答 –

0

來實現,這將是使用MapView的,而不是MapFragment更好的辦法。而這樣做下面

<FrameLayout 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.example.mudasir.login.Maps_Fragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/linearLayout"> 

     <com.google.android.gms.maps.MapView 
      android:id="@+id/mapView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 
    </RelativeLayout> 

</FrameLayout> 

和你Maps_Fragment.java類

public class Maps_Fragment extends Fragment implements OnMapReadyCallback { 

    private View view; 
    private MapView mapView; 
    String[] showroom_addresses = new String[5]; 
    List<Address> addressList = null; 
    LatLng latlng; 
    LatLngBounds.Builder builder = new LatLngBounds.Builder(); 

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

     // Gets the MapView from the XML layout and creates it 
     mapView = (MapView) view.findViewById(R.id.mapView); 
     mapView.onCreate(savedInstanceState); 

     // Set the map ready callback to receive the GoogleMap object 
     mapView.getMapAsync(this);   

     return view; 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 

     // Need to call MapsInitializer before doing any CameraUpdateFactory calls 
     try { 
      MapsInitializer.initialize(this.getActivity()); 
     } catch (GooglePlayServicesNotAvailableException e) { 
      e.printStackTrace(); 
     } 

     mGoogleMap = googleMap; 

     showroom_addresses[0] = "Dubai"; 
     showroom_addresses[1] = "Fujairah"; 
     showroom_addresses[2] = "Al Ain"; 
     showroom_addresses[3] = "Sharjah"; 
     showroom_addresses[4] = "Ras Al Khaimah"; 
     Geocoder geocoder = new Geocoder(getActivity()); 
     for(int i =0; i<5; i++) { 
      try { 
       addressList = geocoder.getFromLocationName(showroom_addresses[i], 1); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      android.location.Address address = addressList.get(0); 
      latlng = new LatLng(address.getLatitude(), address.getLongitude()); 
      Marker marker = mGoogleMap.addMarker(new MarkerOptions().position(latlng).title(showroom_addresses[i])); 
      builder.include(marker.getPosition()); 
     } 

     LatLngBounds bounds = builder.build(); 
     int padding = 0; 
     CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,padding); 
     //CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(new MarkerOptions().position(latlng).title(showroom_addresses[3]).getPosition(),7F); 
     mGoogleMap.animateCamera(cu); 
    } 

    @Override 
    public void onResume() { 
     mapView.onResume(); 
     super.onResume(); 
    } 

    @Override 
    public void onDestroyView() { 
     super.onDestroy(); 
     mapView.onDestroy(); 
    } 
} 

,並請確保您有片段以下的進口和地圖

import android.support.v4.app.Fragment; 

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; 
+0

應用程序仍然崩潰。 java.lang.NullPointerException:嘗試調用null對象引用的接口方法'void maps.ad.zo()'參考 –

+0

您可以發佈錯誤日誌引起的原因 –

+0

java.lang.NullPointerException:試圖調用接口方法'無效的maps.ad.zo()'空對象引用 –

相關問題