2017-10-13 69 views
0

因此,這裏是我的代碼:東西 KRUX是,onMapReady沒有被調用,所以我與無法獲得SupportMapFragment返回anything..seems是總是空

錯誤發揮各地

過程:com.example.herbstzu.listview,PID:16963 kotlin.TypeCastException:空不能轉換到非空型 com.google.android.gms.maps.SupportMapFragment

XML

..... 
<LinearLayout 
      android:id="@+id/LayoutInfo" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginVertical="10dp" 
      android:orientation="horizontal"> 

      <ImageView 
       android:id="@+id/imagePhone2" 
       android:layout_width="60dp" 
       android:layout_height="60dp" 
       android:layout_gravity="start" 
       android:layout_marginLeft="15dp" 
       android:layout_marginRight="15dp" 
       app:srcCompat="@drawable/ic_exclamation" /> 

      <TextView 
       android:id="@+id/info_text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="TextView" 
       android:textColor="#000000" /> 
     </LinearLayout> 

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

    </LinearLayout> 

</ScrollView> 

科特林片段類

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.model.LatLng 
import com.google.android.gms.maps.model.MarkerOptions 
import com.google.android.gms.maps.SupportMapFragment 
import com.google.android.gms.maps.CameraUpdateFactory 
import com.google.android.gms.maps.GoogleMap 
import com.google.android.gms.maps.OnMapReadyCallback 

class Event_details : Fragment(),OnMapReadyCallback{ 

    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 mapFragment = activity.fragmentManager.findFragmentById(R.id.mapF) as SupportMapFragment 
     mapFragment.getMapAsync(this) 

     return myView 
    } 
    override fun onMapReady(googleMap: GoogleMap) { 
     Log.d("onMapready","MapReady") 
     val cphbusiness = LatLng(55.770535, 12.511922) 
     googleMap.addMarker(MarkerOptions().position(cphbusiness).title("Her")) 
     googleMap.moveCamera(CameraUpdateFactory.zoomTo(16.0F)) 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(cphbusiness)) 

    } 
}// Required empty public constructor 

回答

0

您使用了錯誤的片段經理。您需要使用supportFragmentManager

var mapFragment = activity.supportFragmentManager.findFragmentById(R.id.mapF) as SupportMapFragment