簡體   English   中英

onResume 在 onMapReady 之前調用

[英]onResume called before onMapReady

我有一個片段(fragment_search),我想在其中以編程方式添加另一個片段(map_fragment),但我遇到了一個問題,其中 tab_content.onResume() 在 OnMapReadyCallback.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 是錯誤的,但我不知道這樣做的正確方法是什么。 關於我做錯了什么的任何提示? 謝謝!

當您啟動SearchFragment ,它顯然會調用其onResume()方法作為片段生命周期的一部分。 您的日志說onResume()NullPointerException 你應該檢查一下。

關於你的問題,

fragment.getMapAsync(onMapReadyCallback);

onResume()將在您獲得onMapReadyCallback之前被調用,因為getMapAsync()是異步調用並且將在不會阻塞 UI 線程的不同線程中運行,因此onResume()將在您獲得onMapReadyCallback之前被調用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM