1
0

More than 3 years have passed since last update.

Google Maps ActivityをFragmentへコピペするときの注意

Posted at

少しハマったのでメモ

やろうとしたこと

  • Google Maps Activity のコードをそのまま新規FragmentにコピペしてMap表示するFragmentとして使う

image.png

ハマったこと

  • "Unresolved reference: supportFragmentManager" というエラーが出る

対策

  • supportFragmentManager から childFragmentManager に変更

対策前

Fragment.kt
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        val mapFragment = supportFragmentManager
            .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }

対策後

Fragment.kt
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        val mapFragment = childFragmentManager //ここが変わっている
            .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }

原因

  • 階層ごとに呼び出すMapFragmentが違う
    • Activity→Fragment:supportFragmentManager
    • Fragment→子Fragment:childFragmentManager

公式ドキュメントはちゃんと見ないといけないよう

参考になるサイト

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0