Wenn ich versuche, meinen Code auszuführen, habe ich immer diese Fehlermeldung erhalten:kann den Standort nicht der Länge eine Breite
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
ich manuell die Koordinaten in meinem Emulator konfigurieren können, aber immer noch, bekam den gleichen Fehler.
Hier sind meine Codes zum Abrufen von Standortkoordinaten.
LocationTracker.java
public class LocationTracker extends MainActivity implements LocationListener {
MainActivity activity;
Context context;
Location location;
LocationManager lm;
double lat,longi;
boolean isGpsEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetNetworkLocation = false;
public LocationTracker (Context context) {
this.context=context;
getGPSLocation();
}
public Location getGPSLocation() {
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
isGpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGpsEnabled) {
/** Check for RUNTIME PERMISSION */
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (location == null) { //Request for NEW DATA coming from GPS
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);
if (lm != null) { //Retrieve data from the earlier
location=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat= location.getLatitude();
longi= location.getLongitude();
}
}
}
}else {
ActivityCompat.requestPermissions(activity,new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},123);
}
}else {
}
return location;
}
/** GET LATITUDE METHOD */
public double getLatitude() {
if (location != null) {
lat=location.getLatitude();
}
return lat;
}
/** GET LONGITUDE METHOD */
public double getLongitude() {
if (location != null) {
longi=location.getLongitude();
}
return longi;
}
@Override
public void onLocationChanged(Location location) {
lat= location.getLatitude();
longi=location.getLongitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
und MainActivity Klasse für Koordinaten anzeigt.
public class MainActivity extends AppCompatActivity {
Context context;
DBHelper helper;
LocationTracker tracker;
TextView contactsTv,sendEmergencyTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper=new DBHelper(getApplicationContext());
/**
* OnClick Listener in android
*/
contactsTv=(TextView) findViewById(R.id.tv_createContacts);
contactsTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity.this, ContactsActivity.class); //redirect to the COntacts dsadsadsa
startActivity(intent);
}
});
sendEmergencyTv=(TextView)findViewById(R.id.tv_sendEmergency);
sendEmergencyTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tracker= new LocationTracker(MainActivity.this);
double lat,longi;
lat= tracker.getLatitude();
longi= tracker.getLongitude();
Toast.makeText(context,"Latitude: "+lat+ "Longitude: "+longi,Toast.LENGTH_LONG).show();
}
});
}
}
By the way, ich bin nicht sicher im requestPermission Parameter eine MainActivity Instanz innerhalb der LocationTracker Klasse zu platzieren.
Können Sie das Logcat zeigen. –
Ich habe bereits meine Post aktualisiert. danke – PhilipJ