[英]onResume called before onMapReady
I have a fragment(fragment_search) in which I want add another fragment(map_fragment) programmatically, but I'm running into an issue, where tab_content.onResume() is called BEFORE OnMapReadyCallback.onMapReady().我有一个片段(fragment_search),我想在其中以编程方式添加另一个片段(map_fragment),但我遇到了一个问题,其中 tab_content.onResume() 在 OnMapReadyCallback.onMapReady() 之前被调用。
Here's the code:这是代码:
fragment_search.xml : 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: 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 (nothing else in this class really): 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 : 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;
}
...}
Stack trace :堆栈跟踪 :
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) 7)
I think the SearchFragment.onCreateView is wrong, but i don't know what is the correct way of doing it.我认为 SearchFragment.onCreateView 是错误的,但我不知道这样做的正确方法是什么。 Any hints as to what i'm doing wrong?
关于我做错了什么的任何提示? Thanks!
谢谢!
When you launch SearchFragment
, it will apparently call its onResume()
method as a part of the fragment lifecycle.当您启动
SearchFragment
,它显然会调用其onResume()
方法作为片段生命周期的一部分。 Your logs say there is a NullPointerException
in onResume()
.您的日志说
onResume()
有NullPointerException
。 You should check for that.你应该检查一下。
And regarding your question,关于你的问题,
fragment.getMapAsync(onMapReadyCallback);
onResume()
will be invoked before you get onMapReadyCallback
because getMapAsync()
is async call and will run in a different thread which will not block UI thread and hence onResume()
will get invoked before you get onMapReadyCallback
onResume()
将在您获得onMapReadyCallback
之前被调用,因为getMapAsync()
是异步调用并且将在不会阻塞 UI 线程的不同线程中运行,因此onResume()
将在您获得onMapReadyCallback
之前被调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.