Android 安卓融合定位服务崩溃
尝试获取纬度和经度。我正在尝试下面提供的服务。它在尝试启动服务时在设备上崩溃Android 安卓融合定位服务崩溃,android,service,nullpointerexception,location,Android,Service,Nullpointerexception,Location,尝试获取纬度和经度。我正在尝试下面提供的服务。它在尝试启动服务时在设备上崩溃 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.SQLiteExce
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.GoogleAppClient.isConnected()
空对象引用
在调用
之前添加以下代码!mGoogleApiClient.isConnected()
因为您还没有初始化它,并且正在对它调用一个方法
mGoogleApiClient = new GoogleApiClient.Builder(this)
Drive.API,Drive.SCOPE\u文件给我错误请删除错误:java.lang.IllegalArgumentException:必须调用addApi()以添加至少一个API我已更新了AnswerMgoogleapClient=new GoogleapClient.Builder(this).addOnConnectionFailedListener(this).addConnectionCallbacks(this).addApi(LocationServices.API);