2016-03-21 57 views
2

我有一个片段(fragment_search),我想以编程方式添加另一个片段(map_fragment),但我遇到了一个问题,其中tab_content.onResume()被调用了BEFORE OnMapReadyCallback.onMapReady()。onResume在onMapReady之前调用

下面的代码:

fragment_search.xml:

<LinearLayout...> 
    <FrameLayout 
      android:id="@+id/search_map_fragment_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

     </FrameLayout> 
</LinearLayout> 

some_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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"> 

    <com.google.android.gms.maps.MapView android:id="@+id/mapview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:clickable="true" 
    android:enabled="true" 
    android:apiKey="api key" /> 

</LinearLayout> 

SearchFragment.class(闲来无事在这个类真的):

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_search, container, false); 
    FragmentManager fragmentManager = getFragmentManager(); 
    final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

    final SomeFragment fragment = new SomeFragment(); 

    OnMapReadyCallback onMapReadyCallback = new OnMapReadyCallback() { 
     @Override 
     public void onMapReady(GoogleMap googleMap) { 
      fragment.setMap(googleMap); 

      fragmentTransaction.add(R.id.search_map_fragment_container, fragment); 
      fragmentTransaction.commit(); 
     } 
    }; 
    fragment.getMapAsync(onMapReadyCallback); 
    return view; 
} 

个SomeFragment.class:

public class SomeFragment extends SupportMapFragment { 
    MapView mapView; 
    GoogleMap map; 
    private MapView mMapView; 
    private GoogleMap mGoogleMap; 

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

     mMapView = (MapView) v.findViewById(R.id.mapview); 
     mMapView.onCreate(savedInstanceState); 

     return v; 
    } 

    public void setMap(GoogleMap map){ 
     this.mGoogleMap=map; 
    } 
...} 

堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke interface method 'void maps.ei.bz.o()' on a null object reference 
                    at maps.ei.n.b(Unknown Source) 
                    at com.google.android.gms.maps.internal.i$a.onTransact(:com.google.android.gms.alldynamite:115) 
                    at android.os.Binder.transact(Binder.java:387) 
                    at com.google.android.gms.maps.internal.IMapFragmentDelegate$zza$zza.onResume(Unknown Source) 
                    at com.google.android.gms.maps.SupportMapFragment$zza.onResume(Unknown Source) 
                    at com.google.android.gms.dynamic.zza$7.zzb(Unknown Source) 
                    at com.google.android.gms.dynamic.zza.zza(Unknown Source) 
                    at com.google.android.gms.dynamic.zza.onResume(Unknown Source) 
                    at com.google.android.gms.maps.SupportMapFragment.onResume(Unknown Source) 
                    at com.beermeet.ui.search.SearchFragment.onResume(SearchFragment.java:5 

7)

我觉得SearchFragment.onCreateView是错的,但我不知道是什么做的正确方法它。 任何提示,我在做什么错了?谢谢!

回答

1

当您启动SearchFragment时,它显然会将其onResume()方法作为片段生命周期的一部分。 您的日志说明onResume()中有NullPointerException。你应该检查一下。

和关于你的问题,你以前onMapReadyCallback因为getMapAsync()是异步调用

fragment.getMapAsync(onMapReadyCallback); 

onResume()将被调用,并在不同的线程,不会阻塞UI线程,因此onResume()将你之前被调用运行得到onMapReadyCallback

+0

这是非常有道理的。异步加载地图的正确方法是什么? –

+0

你的做法是正确的。根据您的需要,您只需要照顾其他代码流。 –

+0

如果它是正确的,它会工作,对不对? :)) –

相关问题