问题摘要
我在 mapFragment.kt
当我尝试将 MapsActivity 的上下文传递给 mapFragment 内部的方法 isGpsEnable()时,我得到了错误未解决的参考:@MapsActivity
我有一个启动画面 SplashActivity.kt ,该清单位于清单中
我尝试过的
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="gr.mantis_project.obdLogger">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
<activity
android:name="gr.example.obdLogger.SplashActivity"
android:label="@string/title_activity_maps"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="gr.example.obdLogger.MapsActivity">
</activity>
</application>
</manifest>
MapsActivity.kt
package gr.example.obdLogger
class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMapClickListener {
override fun onMapReady(p0: GoogleMap?) {
}
override fun onMapClick(p0: LatLng?) {
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivitymainBinding =
DataBindingUtil.setContentView(this, R.layout.mapsactivity)
setSupportActionBar(findViewById(R.id.toolbar))
}
mapFragment.kt
package gr.example.obdLogger
class MapFragment : Fragment(), OnMapReadyCallback {
private lateinit var mMapView: MapView
private lateinit var mMap: GoogleMap
private lateinit var mView: View
private lateinit var fusedLocationClient: FusedLocationProviderClient
private lateinit var lastLocation: Location
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mMapView.onSaveInstanceState(outState)
isGpsEnable()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_map, container, false)
mMapView = view?.findViewById(R.id.mapview) as MapView
mMapView.onCreate(savedInstanceState)
mMapView.getMapAsync(this)
fusedLocationClient = LocationServices.getFusedLocationProviderClient(requireContext())
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
override fun onResume() {
super.onResume()
mMapView.onResume()
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
mMap.uiSettings.isZoomControlsEnabled = true
}
companion object {
private const val LOCATION_PERMISSION_REQUEST_CODE = 1
}
private fun isGpsEnable(): Boolean {
if (ActivityCompat.checkSelfPermission(
requireContext(), // **This works**
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this@MapsActivity, // **This is unresolved**
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
requestPermissions(
arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
),
LOCATION_PERMISSION_REQUEST_CODE
)
} else {
Log.e("DB", "PERMISSION GRANTED")
}
return true
}
}
答案 0 :(得分:1)
从 MapFragment 到 MapsActivity 没有内部依赖性,因此您不能将MapsActivity引用为外部引用。外部引用仅适用于内部类(请注意,lambda函数是匿名内部类,因此您也可以在其中使用this@
来引用外部范围)。如果您需要在应用程序中使用上下文,则可以在 MapFragment 中调用nullsafe requireContext()
方法或引用 MapsActivity 的实例> 至
(activity as MainActivity)
一个片段可以通过其属性activity
访问与其关联的活动,这就是(activity as MainActivity)
起作用的原因
使用可空值时,我喜欢使用??与let
链接的运算符,例如
context?.let{ doStuff(it) }
这样就可以确保不会出现nullptr异常
答案 1 :(得分:0)
它不起作用。 MapsActivity
不是片段的外部范围。您可以了解更多有关合格this
here