在 onCreate 方法之前调用 onLocationChanged - Android
onLocationChanged call before onCreate method - Android
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.rajitha.model.Point;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
public class CurrentLocationActivity extends AppCompatActivity implements
OnMapReadyCallback, LocationListener {
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private GoogleMap mMap;
ImageView share;
TextView logitude,latitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar tb = findViewById(R.id.toolbar);
share=(ImageView)findViewById(R.id.share);
logitude=(TextView)findViewById(R.id.logitude);
latitude=(TextView)findViewById(R.id.latitude);
setSupportActionBar(tb);
tb.setSubtitle("Your Point");
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.current_location);
mapFragment.getMapAsync(this);
LocationManager mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mLocListener = new CurrentLocationActivity();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocListener);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Test share",logitude.getText().toString());
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMyLocationButtonClickListener(onMyLocationButtonClickListener);
mMap.setOnMyLocationClickListener(onMyLocationClickListener);
enableMyLocationIfPermitted();
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setMinZoomPreference(5);
}
private void enableMyLocationIfPermitted() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
} else if (mMap != null) {
mMap.setMyLocationEnabled(true);
}
}
private void showDefaultLocation() {
Toast.makeText(this, "Point permission not granted, " +
"showing default location",
Toast.LENGTH_SHORT).show();
LatLng redmond = new LatLng(7.8731, 80.7718);
mMap.moveCamera(CameraUpdateFactory.newLatLng(redmond));
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
enableMyLocationIfPermitted();
} else {
showDefaultLocation();
}
return;
}
}
}
private GoogleMap.OnMyLocationButtonClickListener onMyLocationButtonClickListener =
new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
mMap.setMinZoomPreference(5);
return false;
}
};
private GoogleMap.OnMyLocationClickListener onMyLocationClickListener =
new GoogleMap.OnMyLocationClickListener() {
@Override
public void onMyLocationClick(@NonNull Location location) {
mMap.setMinZoomPreference(4);
CircleOptions circleOptions = new CircleOptions();
circleOptions.center(new LatLng(location.getLatitude(),
location.getLongitude()));
circleOptions.radius(200);
circleOptions.fillColor(Color.RED);
circleOptions.strokeWidth(6);
mMap.addCircle(circleOptions);
}
};
@Override
public void onLocationChanged(Location location) {
logitude.setText(String.valueOf(location.getLongitude()));
latitude.setText(String.valueOf(location.getLatitude()));
String message = String.format(
"New Point \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Log.e("Test",message);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
Here I just need to update current longide and latitude values in text. But I got a null exception after running this code. It seems like logitude and latitude TextViews are getting null because of onCreate call after onLocationChanged methos.I need to share those values after clicking buttton .How can I fulfill those task from avove code.Thanks in advance.
你注意到你的 mLocListener 是什么了吗?
移除LocationListener mLocListener = new CurrentLocationActivity();
而不是mLocListener到处使用this。
喜欢改变
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocListener);
到
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.rajitha.model.Point;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
public class CurrentLocationActivity extends AppCompatActivity implements
OnMapReadyCallback, LocationListener {
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private GoogleMap mMap;
ImageView share;
TextView logitude,latitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar tb = findViewById(R.id.toolbar);
share=(ImageView)findViewById(R.id.share);
logitude=(TextView)findViewById(R.id.logitude);
latitude=(TextView)findViewById(R.id.latitude);
setSupportActionBar(tb);
tb.setSubtitle("Your Point");
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.current_location);
mapFragment.getMapAsync(this);
LocationManager mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mLocListener = new CurrentLocationActivity();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocListener);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Test share",logitude.getText().toString());
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMyLocationButtonClickListener(onMyLocationButtonClickListener);
mMap.setOnMyLocationClickListener(onMyLocationClickListener);
enableMyLocationIfPermitted();
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setMinZoomPreference(5);
}
private void enableMyLocationIfPermitted() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
} else if (mMap != null) {
mMap.setMyLocationEnabled(true);
}
}
private void showDefaultLocation() {
Toast.makeText(this, "Point permission not granted, " +
"showing default location",
Toast.LENGTH_SHORT).show();
LatLng redmond = new LatLng(7.8731, 80.7718);
mMap.moveCamera(CameraUpdateFactory.newLatLng(redmond));
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
enableMyLocationIfPermitted();
} else {
showDefaultLocation();
}
return;
}
}
}
private GoogleMap.OnMyLocationButtonClickListener onMyLocationButtonClickListener =
new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
mMap.setMinZoomPreference(5);
return false;
}
};
private GoogleMap.OnMyLocationClickListener onMyLocationClickListener =
new GoogleMap.OnMyLocationClickListener() {
@Override
public void onMyLocationClick(@NonNull Location location) {
mMap.setMinZoomPreference(4);
CircleOptions circleOptions = new CircleOptions();
circleOptions.center(new LatLng(location.getLatitude(),
location.getLongitude()));
circleOptions.radius(200);
circleOptions.fillColor(Color.RED);
circleOptions.strokeWidth(6);
mMap.addCircle(circleOptions);
}
};
@Override
public void onLocationChanged(Location location) {
logitude.setText(String.valueOf(location.getLongitude()));
latitude.setText(String.valueOf(location.getLatitude()));
String message = String.format(
"New Point \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Log.e("Test",message);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
Here I just need to update current longide and latitude values in text. But I got a null exception after running this code. It seems like logitude and latitude TextViews are getting null because of onCreate call after onLocationChanged methos.I need to share those values after clicking buttton .How can I fulfill those task from avove code.Thanks in advance.
你注意到你的 mLocListener 是什么了吗?
移除LocationListener mLocListener = new CurrentLocationActivity();
而不是mLocListener到处使用this。
喜欢改变
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocListener);
到
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);