Android fragments lateinit值始终为null或未使用MapBox初始化
您好,我对android的MapBox SDK有一个问题,我在我的项目中使用kotlin语言,并且Viewmap值总是有问题。我刚到科特林,所以我可能会跳过一些东西 我制作了底部的导航栏,所以我使用片段在我的应用程序中导航 我的片段类是我使用的变量Android fragments lateinit值始终为null或未使用MapBox初始化,android-fragments,kotlin,mapbox,Android Fragments,Kotlin,Mapbox,您好,我对android的MapBox SDK有一个问题,我在我的项目中使用kotlin语言,并且Viewmap值总是有问题。我刚到科特林,所以我可能会跳过一些东西 我制作了底部的导航栏,所以我使用片段在我的应用程序中导航 我的片段类是我使用的变量 package com.example.parky.parky_android import android.app.Application import android.content.Context import android.os.Bundl
package com.example.parky.parky_android
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import android.view.ContextThemeWrapper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import com.mapbox.mapboxsdk.constants.Style
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
class ItemFourFragment : Fragment() {
private lateinit var mapView: MapView
fun onCreate(savedInstanceState: Bundle?, context: Context, inflater: LayoutInflater, container: ViewGroup?) {
super.onCreate(savedInstanceState)
// Mapbox Access token
Mapbox.getInstance(context, getString(R.string.token_mapbox))
val view = inflater.inflate(R.layout.activity_main, container ,false)
mapView = view.findViewById(R.id.mapview)
mapView.onCreate(savedInstanceState)
mapView.getMapAsync({
it.setStyle(Style.SATELLITE)
// Customize map with markers, polylines, etc.
})
}
override fun onStart() {
super.onStart()
mapView.onStart()
}
override fun onResume() {
super.onResume()
mapView.onResume()
}
override fun onPause() {
super.onPause()
mapView.onPause()
}
override fun onStop() {
super.onStop()
mapView.onStop()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView.onSaveInstanceState(outState)
}
override fun onLowMemory() {
super.onLowMemory()
mapView.onLowMemory()
}
override fun onDestroy() {
super.onDestroy()
mapView.onDestroy()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_item_four, container, false)
}
companion object {
fun newInstance(): ItemFourFragment {
val fragment = ItemFourFragment()
return fragment
}
}
}
以及显示xml文件的位置:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ItemFiveFragment">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="41.885"
mapbox:mapbox_cameraTargetLng="-87.679"
mapbox:mapbox_styleUrl="@string/mapbox_style_satellite"
mapbox:mapbox_cameraTilt="20"
mapbox:mapbox_cameraZoom="12"/>
</RelativeLayout>
你知道问题出在哪里吗
谢谢您的时间。这里:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_item_four, container, false)
}
您将返回一个将附加到片段并实际显示的视图
在这里:
您正在膨胀一个从未附加到任何内容的新视图。它还扩展了一个单独的布局,activity\u main.xml
,我假设它不包括MapView
相反,在onCreateView()
中返回膨胀视图后,应该在onViewCreated()
中解析MapView
:
您也不应该对此属性使用lateinit var
,因为您不能保证创建视图(例如,onCreateView()
可能永远不会被调用)。这种情况的一个例子是,如果您的片段位于后台,并且用户不可见。在活动的配置更改或重新创建时,backbackback上的片段将通过onCreate()
,但不会创建视图。在这种情况下,您将在尝试引用尚未初始化的mapView
时在onStart()
中遇到崩溃
相反,请将其保留为空,并使用mapView?.doThing()
执行操作谢谢您的回答,这是问题的一部分,我自己解决了另一部分。谢谢!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_item_four, container, false)
}
fun onCreate(savedInstanceState: Bundle?, context: Context, inflater: LayoutInflater, container: ViewGroup?) {
super.onCreate(savedInstanceState)
// Mapbox Access token
Mapbox.getInstance(context, getString(R.string.token_mapbox))
val view = inflater.inflate(R.layout.activity_main, container ,false)
mapView = view.findViewById(R.id.mapview)
mapView.onCreate(savedInstanceState)
mapView.getMapAsync({
it.setStyle(Style.SATELLITE)
// Customize map with markers, polylines, etc.
})
}
override fun onViewCreated(View view, Bundle savedState) {
mapView = view.findViewById(R.id.mapview).apply {
onCreate(savedState)
getMapAsync { map ->
map.setStyle(Style.SATELLITE)
}
}
}