2017-10-14 101 views
-2

的問題所以我能夠使用MapFragment沒有問題,但是當我將其更改爲supportMapFragment時,它會保留。與下面的錯誤。不知道我在做什麼錯,一個工程,另一個沒有。我需要一個支持的原因是,我不能刪除片段與使用mapfragment,因爲下面的行是expectin片段!並且正在接收MapFragment .....所以它失敗了...當我將它切換到SupportMapFragment時,下面的代碼行可行,但是錯誤發生在下面。從fragmentManager/MapFragment移動到supportfragmentManager/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" /> 

CLASS

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 
    } 

錯誤

n時間:致命異常:主 工藝:com.example.herbstzu.listview,PID:11988 kotlin.TypeCastException:空不能轉換到非空型 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)

SOLUTION

所以...溶液呈兩倍....

當從MapFragment更改爲SupportMapFragment .......

  1. 將XML類更改爲SupportMapFragment中的一個片段
  2. 變化使SupportMapFragment是整個代碼
  3. 變化supportFragmentManager到childFragmentManager..supportFragmentManager一個可空還將工作,但它不會觸發onMapReady ...
  4. 微笑,因爲你拍下來科特林/ Android的像一個小.. ...;)
+0

某些「答案不完整......在此處發佈完整答案... – BostonMacOSX

+0

您可以發佈完整答案並接受它。請參閱[此處](https://stackoverflow.com/help/self -answer) – BakaWaii

回答

0

在kotlin可空類(with?在類類型的末尾)是不同於不可空的類。 findFragmentById可以返回null,因此返回類型爲空。您正嘗試投入不可空的SupportMapFragment

嘗試做這樣activity.supportFragmentManager.findFragmentById(R.id.mapF) as SupportMapFragment?

+0

MapFragment不爲null,因此supportMapFragment也不應該爲空...... – BostonMacOSX

+0

這是通過id找到的文檔'@return如果找到了fragment,否則返回null「。這不是關於它* *是** null或不是,但aboutt **可以是**。我已經更新了答案,以便更清楚。 –

0

因爲你SupportFragment可以爲空,你需要做一個空檢查。有幾種方法可以做到這一點。最簡單的是

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

這會忽略?之後的所有內容。如果該值爲空。

使用

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

的工作方式相同,除了在it,一切是你SupportFragment它不能爲null。

的最後但並非最不重要的是,通過使用

activity.supportFragmentManager.findFragmentById(R.id.mapF) 
(mapFragment as YourFragment).!!getMapAsync(this) 
不推薦由於NullPointerExceptions的

強制空值。

+0

雖然... mapFragment不爲空,所以supportMapFragment shouldn' t也爲空... – BostonMacOSX

+0

看到上面的解決方案...所需的childfragmentmanager ...那麼它現在似乎工作... – BostonMacOSX