Android的融合位置服务崩溃[复制]

问题描述 投票:-2回答:1

这个问题已经在这里有一个答案:

试图让经度和纬度。我想下面给出的服务。它崩溃的设备上,同时试图启动该服务。

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.location.Location;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.model.LatLng;
import com.google.firebase.iid.FirebaseInstanceId;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Turzo on 01-Aug-17.
 */

public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {

    private String TAG = "LocationService";
    private LocationRequest mLocationRequest;
    private GoogleApiClient mGoogleApiClient;
    private static final String LOGSERVICE = "#######";
    private String tempAddress = "";
    private int erroHandling = 0;
    //Context context;
    private LatLng localLatLng;

    PowerManager pm;
    PowerManager.WakeLock wl;
    private int userStatusSendFlag = 0;
    private String locationAuth;
    private String locationAppID;
    private String locationUser;
    //private String lUserAuth;


    DataBaseHelper myDbHelper = new DataBaseHelper(null);
    Cursor cursor;
    private String data_user_id;


    @Override
    public void onCreate() {

        super.onCreate();
        Log.d(LOGSERVICE, "onCreate");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //Log.i(LOGSERVICE, "onStartCommand");
        Toast.makeText(this, "Updating location started!", Toast.LENGTH_SHORT).show();
        Log.d(TAG,"----------Location service started!-----------------");
        // getUser();

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        locationAppID = refreshedToken;

        locationUser = data_user_id;

        if (!mGoogleApiClient.isConnected()) {

            mGoogleApiClient.connect();
        }

        return START_NOT_STICKY;

    }


    @Override
    public void onConnected(Bundle bundle) {
        Log.d(LOGSERVICE, "onConnected" + bundle);

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // 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 ActivityCompat#requestPermissions for more details.
            return;
        }
        Location l = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (l != null) {

            //-- start 1
        }


    }

    @Override
    public void onConnectionSuspended(int i) {

        Log.d(TAG,"----------Location service Connection Suspended!-----------------");

    }

    @Override
    public void onLocationChanged(Location location) {

        double lat = location.getLatitude();
        double lng = location.getLongitude();

        //-- start 2
        Toast.makeText(this,String.valueOf(lat)+"/n"+String.valueOf(lng),Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        //Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
        // Toast.makeText(this, "Updating location stopped!", Toast.LENGTH_SHORT).show();
        Log.d("111111Updating...","---------------------- Service Stopped! ---------------------- ");
        stopSelf();

    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        // Log.i(LOGSERVICE, "onConnectionFailed ");
        Toast.makeText(this, "Connection Failed!", Toast.LENGTH_SHORT).show();

    }

    private void initLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    }


    //--------------------



} 

错误是:

显示java.lang.NullPointerException:通过引起尝试上的空对象引用调用虚拟方法“布尔com.google.android.gms.common.api.GoogleApiClient.isConnected()”

android service nullpointerexception location
1个回答
1
投票

调用!mGoogleApiClient.isConnected()前添加下面的代码。

因为你还没有初始化它尚未您在调用它的方法。

mGoogleApiClient = new GoogleApiClient.Builder(this)
© www.soinside.com 2019 - 2024. All rights reserved.