有问题从fragmentManager / MapFragment移动到supportfragmentManager / SupportMapFragment

所以我能够使用MapFragment没有问题,但是当我将其更改为支持MapFragment的时候,它已经退出了。 与下面的错误。 不知道我做错了,一个工程,另一个没有。 我需要支持的一个原因是,我不能删除使用mapfragment时的片段,因为下面的行是expectin片段! 并正在接收MapFragment …..所以它失败了…当我切换到SupportMapFragment下面的行的作品,但错误发生在下面。

谢谢

getFragmentManager().beginTransaction().remove(mapFragment).commit(); 

XML

  <fragment android:id="@+id/mapF" class="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="200dp" /> 

 import android.location.Address import android.os.Bundle import android.support.v4.app.Fragment import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.google.android.gms.maps.* import com.google.android.gms.maps.model.LatLng import com.google.android.gms.maps.model.MarkerOptions import android.location.Geocoder import android.text.Html import android.webkit.WebView import android.widget.Toast import com.beust.klaxon.JsonObject import com.beust.klaxon.Parser import com.beust.klaxon.obj import com.beust.klaxon.string import com.github.kittinunf.fuel.httpGet import com.github.kittinunf.fuel.httpPost import com.github.kittinunf.result.Result import com.github.kittinunf.result.getAs import kotlinx.android.synthetic.main.fragment_1.view.* import kotlinx.android.synthetic.main.fragment_event_details.* import kotlinx.android.synthetic.main.fragment_event_details.view.* import java.util.* import kotlin.collections.ArrayList class Event_details : Fragment(),OnMapReadyCallback{ lateinit var mMap:GoogleMap lateinit var mWeb:WebView lateinit var mapFragment:SupportMapFragment override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { var myView = inflater!!.inflate(R.layout.fragment_event_details, container, false) var mP = Pair("news_id",101) var mL:ArrayList<Pair<String,Int>> = ArrayList<Pair<String,Int>>() mL.add(mP) ****do some JSON STUFF**** >>>>> LINNE 125 mapFragment = activity.supportFragmentManager.findFragmentById(R.id.mapF) as SupportMapFragment mapFragment.getMapAsync(this) return myView } 

错误

ntime:致命例外:主进程:com.example.herbstzu.listview,PID:11988 kotlin.TypeCastException:null无法在com.example.sadfasdf处转换为非null类型的com.google.android.gms.maps.SupportMapFragment。在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)listview.Event_details.onCreateView(Event_details.kt:125)

所以…解决方案是两倍…

MapFragment更改为SupportMapFragment时 …….

  1. 将XML类更改为片段侧的SupportMapFragment
  2. 更改以使SupportMapFragment在您的代码中可以为空
  3. 更改supportFragmentManager到childFragmentManager..supportFragmentManager也将工作,但它不会触发onMapReady …
  4. 微笑,因为你击倒Kotlin / Android像一点点…..;)

由于您的SupportFragment可能为空,您需要执行空检查。 有几种方法可以做到这一点。 最简单的是

 activity.supportFragmentManager.findFragmentById(R.id.mapF) (mapFragment as SupportFragment)?.getMapAsync(this) 

这会在?之后忽略一切。 如果该值为空。

运用

 activity.supportFragmentManager.findFragmentById(R.id.mapF)?.let { (it as SupportFragment).getMapAsync(this) } 

以同样的方式工作,除了它的一切都是你的SupportFragment不能为空。

最后但并非最不重要的是通过使用强制可空值

 activity.supportFragmentManager.findFragmentById(R.id.mapF) (mapFragment as YourFragment).!!getMapAsync(this) 

这是不推荐由于nullpointerexceptions。

在kotlin可为空的类(与类类型的末尾?)是不同于不可空的类。 findFragmentById可以返回null,所以返回类型是可空的。 您正尝试投入不可空的SupportMapFragment

试试这样做activity.supportFragmentManager.findFragmentById(R.id.mapF) as SupportMapFragment?