Buenas tardes estoy tratando de obtener mi ubicación de mi gps por medio location manager, ya tengo mi clase hecha solo que al momento de ejecutar mi app me manda un error por que tengo android 6.0 y tengo que validad permisos, ya hice mi validadcion pero aun así me manda error
Clase location manager
public class MiServicio extends Service implements LocationListener {
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
private static final int MY_ACCESS_FINE_LOCATION = 1 ;
private static final int MY_ACCESS_COARSE_LOCATION = 1 ;
private final Context context;
double latitud;
double longitud;
Location location;
boolean gpsActivo;
TextView texto;
LocationManager locationManager;
public MiServicio() {
super();
this.context = this.getApplicationContext();
}
public MiServicio(Context context) {
this.context = context;
getLocation();
}
public void setView(View v) {
texto = (TextView) v;
texto.setText("Coordenadas:" + latitud + longitud);
}
public void getLocation() {
try {
locationManager = (LocationManager) this.context.getSystemService(LOCATION_SERVICE);
gpsActivo = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception e) {
}
if (gpsActivo) {
if (ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((MainActivity) context,
Manifest.permission.ACCESS_FINE_LOCATION)) {
} else {
ActivityCompat.requestPermissions((MainActivity) context,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_ACCESS_FINE_LOCATION);
}
if (ActivityCompat.shouldShowRequestPermissionRationale((MainActivity) context,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
} else {
ActivityCompat.requestPermissions((MainActivity) context,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_ACCESS_COARSE_LOCATION);
}
return;
}
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER
, 1000 * 60
, 10
, this);
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
latitud = location.getLatitude();
latitud = location.getLatitude();
}
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
codigo en mi activity principal
public class MainActivity extends Activity {
private static final int MY_ACCESS_FINE_LOCATION = 0;
private View mLayout;
private String comments = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
MiServicio servicio = new MiServicio(getApplicationContext());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
}
return;
}
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState)
{
View rootView = inflater.inflate(R.layout.activity_main, container, false);
MiServicio servicio = new MiServicio(getApplicationContext());
servicio.setView(rootView.findViewById(R.id.ubicacion));
return rootView;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Error que manda
05-13 15:47:34.802 9303-9303/mx.com.oncontrol.localizacion E/AndroidRuntime: FATAL EXCEPTION: main Process: mx.com.oncontrol.localizacion, PID: 9303 java.lang.RuntimeException: Unable to start activity ComponentInfo{mx.com.oncontrol.localizacion/mx.com.oncontrol.localizacion.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)' 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 'int android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference at android.content.ContextWrapper.checkPermission(ContextWrapper.java:637) at android.support.v4.content.ContextCompat.checkSelfPermission(ContextCompat.java:387) at mx.com.oncontrol.localizacion.MiServicio.(MiServicio.java:25) at mx.com.oncontrol.localizacion.MainActivity.onCreate(MainActivity.java:50) 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) 05-13 15:47:39.915 9303-9303/mx.com.oncontrol.localizacion I/Process: Sending signal. PID: 9303 SIG: 9
ContextCompat.checkSelfPermission
en lugar deActivityCompat.checkSelfPermission