【发布时间】:2017-10-14 12:28:53
【问题描述】:
所以我可以毫无问题地使用 MapFragment,但是当我将其更改为 supportMapFragment 时,它就会退出。出现以下错误。不知道我做错了什么,一个有效,另一个无效。我需要支持的原因是在使用 mapfragment 时我无法删除片段,因为下面的行是片段!并且正在接收 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 不能转换为非 null 类型 com.google.android.gms.maps.SupportMapFragment 在 com.example.sadfasdf.listview.Event_details.onCreateView(Event_details.kt:125) 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:2354) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
解决方案
所以......解决方案有两个......
当从 MapFragment 更改为 SupportMapFragment.......
- 将 XML 类更改为片段一侧的 SupportMapFragment
- 更改以使 SupportMapFragment 在整个代码中都可以为空
- 将 supportFragmentManager 更改为 childFragmentManager..supportFragmentManager 也将起作用,但它不会触发 onMapReady...
- 微笑,因为你打败了 Kotlin/Android 有点像..... ;)
【问题讨论】:
-
一些“答案不完整....在这里发布了完整的答案...
-
您可以发布您的完整答案并接受它。参考here。
标签: android google-maps kotlin supportmapfragment mapfragment