为了账号安全,请及时绑定邮箱和手机立即绑定

在地图上工作时出现空指针异常(新手),但实际上不是指向任何空值

在地图上工作时出现空指针异常(新手),但实际上不是指向任何空值

慕哥9229398 2021-05-07 22:31:15
我可以发送整个项目或所需的文件,请帮助我,这是我的程序`package com.parkaspot.najeeb.project;import android.Manifest;import android.content.Context;import android.content.Intent;import android.content.pm.PackageManager;import android.location.Location;import android.location.LocationManager; import android.support.v4.app.ActivityCompat; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.github.ivbaranov.mli.MaterialLetterIcon; import java.util.ArrayList; import java.util.Random;public class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder>{private Context context;private ArrayList<RecentData> mArrayList;private LayoutInflater inflater;private int[] mMaterialColors;Location networkLocation;Double lati,lngi;private static final Random RANDOM = new Random();public RecentAdapter(Context context, ArrayList<RecentData> list) {    this.context = context;    this.mArrayList = list;    inflater = LayoutInflater.from(context);    mMaterialColors = context.getResources().getIntArray(R.array.colors);    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);    boolean networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);    if (networkEnabled) {        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {            return;        }        networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);    }    if (networkLocation!=null){        lati = networkLocation.getLatitude();        lngi = networkLocation.getLongitude();    }}兄弟们请帮助我,我已经花了3天的时间来解决此问题,但是我无法对Android Programmking进行新手学习,并且正在做一个大学项目,请帮助我修复它
查看完整描述

1 回答

?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

我为什么会看到您的gps无法获得您的位置,并且总是收到null指针,


这是我想获取地理位置时使用的类


LocationDetector.kt


class LocationDetector(val context: Context) {


    val fusedLocationClient: FusedLocationProviderClient = FusedLocationProviderClient(context)

    var locationListener: LocationListener? = null


    interface LocationListener {

        fun locationFound(location: Location)

        fun locationNotFound(reason: String)

    }


    fun detectLocation() {


        //create request

        val locationRequest = LocationRequest()

        locationRequest.interval = 0L


        // check for permission

        val permissionResult = ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION)


        // if have permission, try to get location within 10 seconds

        if (permissionResult == android.content.pm.PackageManager.PERMISSION_GRANTED) {

            val timer = Timer()


            val locationCallback = object : LocationCallback() {

                override fun onLocationResult(locationResult: LocationResult) {

                    fusedLocationClient.removeLocationUpdates(this)

                    timer.cancel()

                    // return location

                    locationListener?.locationFound(locationResult.locations.first())

                }

            }


            timer.schedule(timerTask {

                fusedLocationClient.removeLocationUpdates(locationCallback)

                locationListener?.locationNotFound("Timeout")

            }, 10 * 1000) //10 seconds


            // make request

            fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null)

        } else {

            // if no permission

            locationListener?.locationNotFound("No permission given")

        }

    }

以及你会怎样的方式


class MainActivity : AppCompatActivity(), LocationDetector.LocationListener {

    var isGPSRunning = false

    override fun locationFound(location: Location) {

   AppPreferencesSingleton(applicationContext).put(AppPreferencesSingleton.Key.latitude,location.latitude.toString())

    AppPreferencesSingleton(applicationContext).put(AppPreferencesSingleton.Key.longitude,location.longitude.toString())

}


override fun locationNotFound(reason: String) {

    when(isGPSEnabled()){

        true -> {

            println("Waiting for GPS fix")

        }

        false -> {

            if (!isGPSRunning) {

                isGPSRunning = true

                startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))

            }

        }

    }

}


fun isGPSEnabled() = (getSystemService(Context.LOCATION_SERVICE) as LocationManager).isProviderEnabled(LocationManager.GPS_PROVIDER)

这在KOTLIN中,但是修改很简单,请记住在清单中设置所需的权限:


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <uses-permission android:name="android.permission.INTERNET" />


查看完整回答
反对 回复 2021-05-19
  • 1 回答
  • 0 关注
  • 154 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信