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

我可以发送整个项目或所需的文件,请帮助我,这是我的程序


`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进行新手学习,并且正在做一个大学项目,请帮助我修复它


慕哥9229398
浏览 155回答 1
1回答

一只萌萌小番薯

我为什么会看到您的gps无法获得您的位置,并且总是收到null指针,这是我想获取地理位置时使用的类LocationDetector.ktclass LocationDetector(val context: Context) {&nbsp; &nbsp; val fusedLocationClient: FusedLocationProviderClient = FusedLocationProviderClient(context)&nbsp; &nbsp; var locationListener: LocationListener? = null&nbsp; &nbsp; interface LocationListener {&nbsp; &nbsp; &nbsp; &nbsp; fun locationFound(location: Location)&nbsp; &nbsp; &nbsp; &nbsp; fun locationNotFound(reason: String)&nbsp; &nbsp; }&nbsp; &nbsp; fun detectLocation() {&nbsp; &nbsp; &nbsp; &nbsp; //create request&nbsp; &nbsp; &nbsp; &nbsp; val locationRequest = LocationRequest()&nbsp; &nbsp; &nbsp; &nbsp; locationRequest.interval = 0L&nbsp; &nbsp; &nbsp; &nbsp; // check for permission&nbsp; &nbsp; &nbsp; &nbsp; val permissionResult = ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION)&nbsp; &nbsp; &nbsp; &nbsp; // if have permission, try to get location within 10 seconds&nbsp; &nbsp; &nbsp; &nbsp; if (permissionResult == android.content.pm.PackageManager.PERMISSION_GRANTED) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val timer = Timer()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val locationCallback = object : LocationCallback() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override fun onLocationResult(locationResult: LocationResult) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fusedLocationClient.removeLocationUpdates(this)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer.cancel()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // return location&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; locationListener?.locationFound(locationResult.locations.first())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer.schedule(timerTask {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fusedLocationClient.removeLocationUpdates(locationCallback)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; locationListener?.locationNotFound("Timeout")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, 10 * 1000) //10 seconds&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // make request&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null)&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if no permission&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; locationListener?.locationNotFound("No permission given")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }以及你会怎样的方式class MainActivity : AppCompatActivity(), LocationDetector.LocationListener {&nbsp; &nbsp; var isGPSRunning = false&nbsp; &nbsp; override fun locationFound(location: Location) {&nbsp; &nbsp;AppPreferencesSingleton(applicationContext).put(AppPreferencesSingleton.Key.latitude,location.latitude.toString())&nbsp; &nbsp; AppPreferencesSingleton(applicationContext).put(AppPreferencesSingleton.Key.longitude,location.longitude.toString())}override fun locationNotFound(reason: String) {&nbsp; &nbsp; when(isGPSEnabled()){&nbsp; &nbsp; &nbsp; &nbsp; true -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; println("Waiting for GPS fix")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; false -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!isGPSRunning) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isGPSRunning = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}fun isGPSEnabled() = (getSystemService(Context.LOCATION_SERVICE) as LocationManager).isProviderEnabled(LocationManager.GPS_PROVIDER)这在KOTLIN中,但是修改很简单,请记住在清单中设置所需的权限:<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />&nbsp; &nbsp; <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />&nbsp; &nbsp; <uses-permission android:name="android.permission.INTERNET" />
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java