无法访问“<init>”:它在“LocationRequest”中是私有的

Posted

技术标签:

【中文标题】无法访问“<init>”:它在“LocationRequest”中是私有的【英文标题】:Cannot access '<init>': it is private in 'LocationRequest' 【发布时间】:2021-11-17 05:57:09 【问题描述】:

我是 Kotlin 的新手,我正在尝试实时获取位置并在 toast 中显示坐标。问题是我无法继续使用代码,因为它没有检测到 IniciartLocationRequest() 函数中的 LocationRequest ()。有什么想法吗?

下面的代码是mainActivity(还有待完成)

package com.example.ubicacion

import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.location.LocationRequest
import android.os.Bundle
import android.os.Looper
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices

class MainActivity : AppCompatActivity() 

    private val permisoFineLocation = android.Manifest.permission.ACCESS_FINE_LOCATION
    private val permisoCoarseLocation = android.Manifest.permission.ACCESS_COARSE_LOCATION

    private val codigoSolicitudPermiso = 100

    private lateinit var fusedLocationClient: FusedLocationProviderClient
    private lateinit var locationRequest: LocationRequest

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
        iniciarLocationRequest()
    

    private fun iniciarLocationRequest() 
        locationRequest = LocationRequest()


    

    private fun validarPermisosUbicacion(): Boolean 
        val hayUbicacionPrecisa = ActivityCompat.checkSelfPermission(
            this,
            permisoFineLocation
        ) == PackageManager.PERMISSION_GRANTED
        val hayUbicacionOrdinaria = ActivityCompat.checkSelfPermission(
            this,
            permisoCoarseLocation
        ) == PackageManager.PERMISSION_GRANTED

        return hayUbicacionPrecisa && hayUbicacionOrdinaria
    

    @SuppressLint("MissingPermission")
    private fun obtenerUbicacion() 
        /*fusedLocationClient?.lastLocation?.addOnSuccessListener(this,
            object : OnSuccessListener<Location> 
                override fun onSuccess(p0: Location?) 
                    if (p0 != null) 
                        Toast.makeText(
                            applicationContext,
                            p0.latitude.toString() + "-" + p0.longitude.toString(),
                            Toast.LENGTH_LONG
                        ).show()
                    
                
            )*/

        val callback = object : LocationCallback() 

            override fun onLocationResult(p0: LocationResult) 
                super.onLocationResult(p0)
                for (ubicacion in p0.locations) 
                    Toast.makeText(applicationContext, "a", Toast.LENGTH_SHORT).show()
                
            
        
        fusedLocationClient.requestLocationUpdates(
            locationRequest,
            callback,
            Looper.getMainLooper()
        )
    

    private fun pedirPermisos() 
        val deboProveerContexto =
            ActivityCompat.shouldShowRequestPermissionRationale(this, permisoFineLocation)

        if (deboProveerContexto) 
            //Mandar mensaje con expliacion adicinal
            solicitudPermiso()
         else 
            solicitudPermiso()
        
    

    private fun solicitudPermiso() 
        requestPermissions(
            arrayOf(permisoCoarseLocation, permisoFineLocation),
            codigoSolicitudPermiso
        )
    

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) 
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        when (requestCode) 
            codigoSolicitudPermiso -> 
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
                    //obtener Ubicacion
                    obtenerUbicacion()
                 else 
                    Toast.makeText(
                        this,
                        "No se dio permiso para acceder a la ubicacion",
                        Toast.LENGTH_SHORT
                    ).show()
                
            
        
    

    override fun onStart() 
        super.onStart()

        if (validarPermisosUbicacion()) 
            obtenerUbicacion()
         else 
            pedirPermisos()
        
    

【问题讨论】:

【参考方案1】:

你从LocationRequest.Builder得到LocationRequest

【讨论】:

还有什么其他方法可以调用我拥有的函数?因为无论如何我总是会出错

以上是关于无法访问“<init>”:它在“LocationRequest”中是私有的的主要内容,如果未能解决你的问题,请参考以下文章

无法使用虚拟主机访问 wp-admin

tomcat启动

无法访问密码:它在用户中是私有的

如何在java中使用Filter进行访问控制

Pandas Series .loc() 附加后访问错误

获取 PermissionError:[WinError 32] 进程无法访问该文件,因为它在 scikitlearn GridSearchCV 期间正被另一个进程使用