【问题标题】:getSupportFragmentManager().findFragmentById() always returns nullgetSupportFragmentManager().findFragmentById() 总是返回 null
【发布时间】:2015-02-20 16:41:57
【问题描述】:

我正在尝试在我的应用程序中显示 GoogleMap,但我在此任务开始时遇到了问题。 行:

SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

总是返回 null。

我尝试了很多东西,但没有任何效果。你能帮我找出问题吗?

这是我的代码:

public class MapSectionFragment extends Fragment implements
    ConnectionCallbacks,
    OnConnectionFailedListener,
    LocationListener,
    OnMyLocationButtonClickListener,
    OnMapReadyCallback{

    private GoogleMap map;
    Location location;
    GoogleApiClient mGoogleApiClient;

    private static final LocationRequest REQUEST = LocationRequest.create()
        .setInterval(5000)         // 5 seconds
        .setFastestInterval(16)    // 16ms = 60fps
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    private void createMapView(){
        try{
            //here it comes
            SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

            map = mapFrag.getMap(); //throws NullPointerException
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_section_map,
            container, false);

        createMapView();

        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        mGoogleApiClient.disconnect();
    }

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

    @Override
    public void onConnected(Bundle bundle) {
    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient,
            REQUEST,
            this);  // LocationListener
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map.setMyLocationEnabled(true);
    }

    @Override
    public boolean onMyLocationButtonClick() {
        Toast.makeText(getActivity(), "MyLocation button clicked", Toast.LENGTH_SHORT).show();
        // Return false so that we don't consume the event and the default behavior still occurs
        // (the camera animates to the user's current position).
    return false;
    }
}

这里是带有 mapID 的 XML“fragment_section_map”:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">


    <Button
        android:id="@+id/refresh_map_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="@string/refresh_map" />

    <fragment
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</LinearLayout>

感谢您的帮助!

【问题讨论】:

  • 显然地图片段不在活动的片段管理器中......显然至少有两个原因......首先:它是你的片段的子片段......其次onCreateView没有在里面完成,嗯,嗯,onCreateView ...所以视图没有添加到活动中......所以地图片段没有添加到活动中..
  • @Selvin 我是新手,不太擅长这个。你能详细解释一下我能解决我的问题吗?谢谢
  • 你必须学习Fragment's lifecycle ...换句话说,使用不同的回调不是onCreateView而是onActivityCreated或onStart ...检查示例应用程序以获取地图

标签: java android android-fragments google-maps-android-api-2


【解决方案1】:

请试试这个替换

SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

因为

getChildFragmentManager()

将在片段内管理片段,因为您在片段内使用 SupportMapFragment,所以请尝试此解决方案

希望对你有帮助

【讨论】:

  • 很高兴听到这个消息:)
  • 搜索 30 分钟后找到了这个答案。谢谢!
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 2012-06-05
相关资源
最近更新 更多