简体   繁体   English

尽管使用了请求位置更新,仍将Location.getLatitude()显示为空对象?

[英]Shows Location.getLatitude() a null object despite use of Request location updates?

I am a newbie to android programming and i have been attempting to use geofence to activate alaram services in my app. 我是android编程的新手,我一直在尝试使用geofence激活我的应用程序中的alaram服务。 It worked fine until one day it showed Null pointer reference to double.getlatitude(). 直到一天它显示对double.getlatitude()的Null指针引用,它都可以正常工作。 I have searched websites with no result. 我搜索了没有结果的网站。 i have used requestLocationUpdates to get updates from the user location. 我已经使用requestLocationUpdates从用户位置获取更新。 Here is my code- 这是我的代码-

package saksham.geofencing;

import android.Manifest;
import android.app.Service;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.location.Geofence;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.ArrayList;

public class MapsActivity extends FragmentActivity implements LocationListener{

    private GoogleMap googleMap; // Might be null if Google Play services APK is not available.
    public SQLiteDatabase db;
    ArrayList<Geofence> mGeofences;
    public double latitude=77.80;
    public double longitude=55.76;
    Double valueindex=0.0;
    private int request=0;
    /**
     * Geofence Coordinates
     */
    ArrayList<LatLng> mGeofenceCoordinates;
    /**
     * Geofence Store
     */
    private GeofenceStore mGeofenceStore;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGeofences = new ArrayList<Geofence>();
        mGeofenceCoordinates = new ArrayList<LatLng>();
        db = openOrCreateDatabase("CoordsDB", Context.MODE_PRIVATE, null);
//        db.execSQL("CREATE TABLE IF NOT EXISTS Coordinates(number DOUBLE,latitude DOUBLE,longitude DOUBLE);");
//        db.execSQL("INSERT INTO Coordinates VALUES('Fixed', 28.61,77.20)");
        setContentView(R.layout.activity_maps);
        SupportMapFragment supportMapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
        /**
         * SupportMapFragment belongs to the v4 support library, contrary to the default MagFragment that is a native component in Android.
         SupportMapFragment will support more Android versions, but it is also an additional library you have to add in your project,
         so I think it really depends on the Android versions you are targeting:
         •  On recent versions, the default components should be enough
         •  On older versions you will need to install the v4 support library and maybe others
         *
         */
        googleMap = supportMapFragment.getMap();
        Log.i("My activity", "maps=" + googleMap);
        googleMap.setMyLocationEnabled(true);
        /**
         * setMyLocationEnabled(true/false) shows the true location when the GPS is switched on from the device. It is an inbuilt feature of the googlemaps .
         */

        LocationManager locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
        // getting GPS status
        boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        Log.i("My activity", "gps is" +isGPSEnabled);

        // getting network status
        boolean isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        Log.i("My activity", "network is" +isNetworkEnabled);

        Criteria crta = new Criteria();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) {
            crta.setAccuracy(Criteria.ACCURACY_FINE);
        }else{
            crta.setAccuracy(Criteria.ACCURACY_MEDIUM);
        }
        /**
         * we have used .setAccuracy as fine for higher SDks than gingerbread .Gingerbread is used as a reference because in apks lower
         * than gingerbread there is very poor geofencing, with gingerbread google made it a lot easier for locationservices to be used for devleopers.
         * it had improved set of tools for Location Services, which included geofencing and substantially improved location discovery.
         */
        crta.setPowerRequirement(Criteria.POWER_LOW);
        String provider = locationManager.getBestProvider(crta, true);

        /**
         * It request Location updates after every 5 sec or if the user traveled 10m
         */
        Log.i("My activity", "manager is " + locationManager);
        Log.i("My activity", "provider is " + provider);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                    MapsActivity.this.requestPermissions(new  String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100);
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for Activity#requestPermissions for more details.
                return;
            }
        }
        Location location = locationManager.getLastKnownLocation(provider);
        Log.i("Location is", location + "");



        if (location != null) {

            onLocationChanged(location);
        }
        else
        {
            locationManager.requestLocationUpdates(provider,
                    ( 1000),0, this);
            Log.i("REached here","here");
            onLocationChanged(location);

        }
        /**
         * the Permission is requested after every 5 sec or at a distance of 2 metres by the user.
         */

    }
    @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
                switch (requestCode) {
                        case 100: {
                                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                                        Toast.makeText(this, "Thanks for the permission", Toast.LENGTH_LONG).show();
                                        // permission was granted, yay! do the
                                                // calendar task you need to do.
                                                    } else {
                                        // permission denied, boo! Disable the
                                                // functionality that depends on this permission.
                                                        Toast.makeText(this, "You did not allow to access your current location", Toast.LENGTH_LONG).show();
                                    }
                           }
                     // other 'switch' lines to check for other
                              // permissions this app might request
                                }
         }


    @Override
    public void onLocationChanged(Location location) {
                 CameraPosition INIT =
                new CameraPosition.Builder()
                        .target(new LatLng(location.getLatitude(),location.getLongitude()))
                        .zoom( 17.5F )
                        .bearing( 300F) // orientation
                        .tilt( 50F) // viewing angle
                        .build();
        // use GooggleMap mMap to move camera into position
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(INIT));
    }
    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
//        mGeofenceStore.disconnect();
        super.onStop();
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
    public  void Add(View view) {
        if (request <= 3) {
            mGeofenceCoordinates.add(new LatLng(latitude, longitude));
            Log.i("The id is", "" + valueindex);
            mGeofences.add(new Geofence.Builder()
                    // The coordinates of the center of the geofence and the radius in meters.
                    .setRequestId("" + valueindex)
                    .setCircularRegion(latitude, longitude, 30)
                    .setExpirationDuration(Geofence.NEVER_EXPIRE)
                            // Required when we use the transition type of GEOFENCE_TRANSITION_DWELL
                    .setLoiteringDelay(50000)
                    .setTransitionTypes(
                            Geofence.GEOFENCE_TRANSITION_ENTER
                                    | Geofence.GEOFENCE_TRANSITION_DWELL
                                    | Geofence.GEOFENCE_TRANSITION_EXIT).build());
            mGeofenceStore = new GeofenceStore(this, mGeofences);
            valueindex++;
            request++;

//            Cursor c = db.rawQuery("SELECT * FROM Coordinates WHERE Id='"+valueindex+"'", null);



            googleMap.addMarker(new MarkerOptions().snippet("Radius:30m").draggable(false).title(valueindex + "").position(new LatLng(latitude,longitude)));
        }
        else{
            Toast.makeText(this,"Maximum limit exceeded",Toast.LENGTH_LONG).show();
        }
    }

}

and here is my Logcat- 这是我的Logcat-

Process: saksham.geofencing, PID: 12102
    java.lang.RuntimeException: Unable to start activity ComponentInfo{saksham.geofencing/saksham.geofencing.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5443)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
            at saksham.geofencing.MapsActivity.onCreate(MapsActivity.java:122)
            at android.app.Activity.performCreate(Activity.java:6245)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5443)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-24 02:11:14.268  12102-12102/saksham.geofencing I/Process﹕ Sending signal. PID: 12102 SIG: 9

please help!!! 请帮忙!!!

You are manually calling onLocationChanged() with a null value in the else branch here: 您正在此处else分支中以空值手动调用onLocationChanged()

if (location != null) {
    onLocationChanged(location);
}
else
{
    locationManager.requestLocationUpdates(provider,
            ( 1000),0, this);
    Log.i("REached here","here");
    // location is null here
    onLocationChanged(location);
}

When you request location updates, the onLocationChanged callback will be called by the LocationManager. 当您请求位置更新时,LocationManager将调用onLocationChanged回调。

The LocationListener which you're implementing is from the google api, that import is this: 您要实现的LocationListener来自google api,该导入是这样的:

import com.google.android.gms.location.LocationListener;

and not this: 而不是这样:

import android.location.LocationListener;

It's also important that the LocationClient is connected before you do this. 在执行此操作之前,必须先连接LocationClient,这一点也很重要。 Also don't call it in the onCreate or onStart methods, but in onResume. 也不要在onCreate或onStart方法中调用它,而是在onResume中调用它。 For more detail go through: https://developer.android.com/training/location/index.html 有关更多详细信息,请访问: https : //developer.android.com/training/location/index.html

Use the fused location api ... this is from an intent service so you will need to just pick out the pieces you need... also are you using the emulator? 使用融合的位置api ...这是从intent服务获得的,因此您只需要挑选所需的部分即可...也正在使用模拟器吗? if so the last known location always used to give me trouble/null values.. are you telneting in and using geofix? 如果是这样,最后一个已知的位置总是用来给我麻烦/空值..您是否正在远程登录并使用geofix?

package geoimage.ret.geoimage;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * <p/>
 * TODO: Customize class - update intent actions, extra parameters and static
 * helper methods.
 */


   public class GetLocationService extends IntentService implements    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
    // TODO: Rename actions, choose action names that describe tasks that this
    // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS

    private static final String ACTION_LOCATION = "location";
    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;


    public GetLocationService() {
        super("GetLocationService");
    }


    /**
     * Starts this service to perform action Baz with the given parameters. If
     * the service is already performing a task this action will be queued.
     *
     * @see IntentService
     */
    // TODO: Customize helper method
    public static void startGetLocation(Context context) {
        Intent intent = new Intent(context, GetLocationService.class);
        intent.setAction(ACTION_LOCATION);
        context.startService(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_LOCATION.equals(action)) {
                handleLocation();
            }
        }
    }


    /**
     * Handle action Baz in the provided background thread with the provided
     * parameters.
     */
    private void handleLocation() {
        // TODO: Handle action Baz
        if (mGoogleApiClient == null) {
            buildGoogleApiClient();
        }
        mGoogleApiClient.connect();

    }


    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }


    @Override
    public void onConnected(Bundle bundle) {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1);
        mLocationRequest.setFastestInterval(1);
        mLocationRequest.setNumUpdates(1);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        sendBroadcast(connectionResult.getErrorMessage());
    }

    @Override
    public void onLocationChanged(Location location) {


        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

        App.setLat(location.getLatitude());
        App.setLon(location.getLongitude());
        mGoogleApiClient.disconnect();
        sendBroadcast("LocationObtained");


    }

    private void sendBroadcast(String errStatus) {
        Intent intent = new Intent("location");
        intent.putExtra("status", errStatus);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        stopSelf();
        }


    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 空对象引用上的Location.getLatitude() - Location.getLatitude() on a null object reference FusedLocationProviderClient:null object 参考上的 Location.getLatitude() - FusedLocationProviderClient : Location.getLatitude() on a null object reference Location.getLatitude等不断变化 - Location.getLatitude etc. keeps changing Android Google Maps-NullPointerException-Location.getLatitude() - Android Google Maps - NullPointerException - Location.getLatitude() 我需要一个蓝点位置,没有位置。getLatitude/ Longtitude - I need a blue dot location, no location.getLatitude/Longtitude Google API v2 location.getLatitude()返回nullpointerException - Google API v2 location.getLatitude() return nullpointerexception 空对象引用上的double android.location.Location.getLatitude()&#39; - double android.location.Location.getLatitude()' on a null object reference 空对象引用ParseGeoPoints上的&#39;double android.location.Location.getLatitude()&#39; - 'double android.location.Location.getLatitude()' on a null object reference ParseGeoPoints Location.getLatitude方法会传回非精密的double,这对GPS座标不利 - Location.getLatitude method returns non-precisioned double which is bad for GPS cordinates 如何将(location.getLatitude())的值赋给变量“ x”,以便我可以对其进行算术运算 - How to assing the value of (location.getLatitude()) to the variable “x” so I can make arithmetic operations with it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM