我想创建一个包含位置更新侦听器的地图应用。我创造了一个,它工作正常,但只在印度,当我尝试在美国使用它会崩溃。我不知道发生了什么。我发送的apk文件给我住在美国的朋友。他有一台HTC设备。
我已经用以下设备检查了它:
在所有这些设备中,我的代码工作正常。
这是我的完整代码:
public class MapScreen extends FragmentActivity implements ConnectionCallbacks,OnConnectionFailedListener, LocationListener{
static LatLng CURRENT , SearchedPosition, LocationsAll;
private GoogleMap map;
EditText search;
// TextView TitleTxt;
// Boolean IsCallingLastSeen = true;
//Location
String currentUserdetailStr;
public Marker currentUser;
private static final String TAG = MapScreen.class.getSimpleName();
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
private Location mLastLocation;
// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
// boolean flag to toggle periodic location updates
private boolean mRequestingLocationUpdates = false;
private LocationRequest mLocationRequest;
// Location updates intervals in sec
private static int UPDATE_INTERVAL = 10000; // 10 sec
private static int FATEST_INTERVAL = 5000; // 5 sec
private static int DISPLACEMENT = 10; // 10 meters
///
Typeface font;
SessionManager session;
RelativeLayout titleBar;
//List<String> ll_StrNew;
LinearLayout SearchLayout;
CheckBox ShowSearch;
JSONObjParser resultis;
JSONObject jsonObj;
String TheamColorDark = "0,187,210",phonenumber,stringLat,stringLong;
String[] sinptArray,phonenumberAry;
ImageView LogOutTxt;
String stringLatitude;
String stringLongitude;
String searchStatus = "";
int aColor,rColor,gColor,bColor;
Double lat,lng;
String[] colorArry;
LatLng getLatLng;
String currntAddress= "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.map_screen);
// addressAry.clear();
try {
// First we need to check availability of play services
if (checkPlayServices()) {
// Building the GoogleApi client
buildGoogleApiClient();
createLocationRequest();
}
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), "checkPlayServices", 1000).show();
}
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
map = mapFragment.getMap();
//LocationsAll
session = new SessionManager(getApplicationContext());
//ll_StrNew = LoginScreen.ll_Str;
// TitleTxt = (TextView) findViewById(R.id.TitleTxtMap);
LogOutTxt = (ImageView) findViewById(R.id.LogoutImageView);
titleBar = (RelativeLayout) findViewById(R.id.TitleBarMap);
SearchLayout = (LinearLayout) findViewById(R.id.SearchLayout);
ShowSearch = (CheckBox) findViewById(R.id.CheckBoxSearch);
Log.w("session == ", session.pref.getString("login_status", "").toString());
try {
GPSTracker gpsTracker = new GPSTracker(this);
if (gpsTracker.canGetLocation())
{
stringLatitude = String.valueOf(gpsTracker.latitude);
stringLongitude = String.valueOf(gpsTracker.longitude);
}
else
{
gpsTracker.showSettingsAlert();
}
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), "gpsTracker", 1000).show();
}
try {
Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
togglePeriodicLocationUpdates();
}
}, 5000);
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), "togglePeriodicLocationUpdates", 1000).show();
}
CURRENT = (new LatLng(locationsLat.get(locationsLat.size()-1), locationsLong.get(locationsLat.size()-1)));
font = LoginScreen.FinalfontFamily;
// TitleTxt.setTypeface(font);
search=(EditText) findViewById(R.id.searchBarEdit);
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(CURRENT, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(18), 1000, null);
// map.isMyLocationEnabled();
// map.setMyLocationEnabled(true);
currentUser = map.addMarker(new MarkerOptions()
.position(CURRENT)
.title(currentUserdetailStr)
.snippet("+1 "+main_numberAry.get(main_numberAry.size()-1))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.current_pin)));
// Drawing circle on the map
drawCircle(CURRENT);
}
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
String strPhone = arg0.getSnippet().toString().replaceAll("-", "");
if (strPhone.equals("Not available") ) {
}
else
{
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("smsto:"));
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address" , strPhone);
smsIntent.putExtra("sms_body" , "Test SMS");
try {
startActivity(smsIntent);
Log.i("Finished sending SMS...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MapScreen.this,
"SMS faild, please try again later.", Toast.LENGTH_SHORT).show();
}
}
}
});
map.setInfoWindowAdapter(new InfoWindowAdapter() {
// Use default InfoWindow frame
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
// Defines the contents of the InfoWindow
@Override
public View getInfoContents(Marker arg0) {
// Getting view from the layout file info_window_layout
View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
// Getting reference to the TextView to set name
TextView PersonName = (TextView) v.findViewById(R.id.personName);
// Getting reference to the TextView to set phone
TextView phone = (TextView) v.findViewById(R.id.PhoneNumberTextView);
PersonName.setText(arg0.getTitle());
phone.setText(arg0.getSnippet());
phone.setTypeface(LoginScreen.FinalfontFamily);
PersonName.setTypeface(LoginScreen.FinalfontFamily);
// Returning the view containing InfoWindow contents
return v;
}
});
}
private void drawMarker(LatLng point, String name, String phone){
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
markerOptions.position(point);
markerOptions.title(name);
markerOptions.snippet(phone);
// Adding marker on the Google Map
map.addMarker(markerOptions);
}
@Override
protected void onStart() {
super.onStart();
try {
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
protected void onResume() {
super.onResume();
try {
checkPlayServices();
// Resuming the periodic location updates
if (mGoogleApiClient.isConnected() && mRequestingLocationUpdates) {
startLocationUpdates();
}
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
protected void onStop() {
super.onStop();
try {
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
protected void onPause() {
super.onPause();
try {
stopLocationUpdates();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* Method to display the location on UI
* */
private void displayLocation() {
try {
mLastLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
// Toast.makeText(getApplicationContext(), "location changed", 1000).show();
new LastSeenProgressTask(MapScreen.this,latitude,longitude).execute();
} else {
Toast.makeText(getApplicationContext(), "(Couldn't get the location. Make sure location is enabled on the device)", 1000).show();
}
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* Method to toggle periodic location updates
* */
private void togglePeriodicLocationUpdates() {
try {
if (!mRequestingLocationUpdates) {
// Changing the button text
mRequestingLocationUpdates = true;
// Starting the location updates
startLocationUpdates();
Log.d(TAG, "Periodic location updates started!");
}
else {
// Changing the button text
// btnStartLocationUpdates
// .setText(getString(R.string.btn_start_location_updates));
mRequestingLocationUpdates = false;
// Stopping the location updates
stopLocationUpdates();
Log.d(TAG, "Periodic location updates stopped!");
}
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), "togglePeriodicLocationUpdates function", 1000).show();
}
}
/**
* Creating google api client object
* */
protected synchronized void buildGoogleApiClient() {
try {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), "GoogleApiClient.Builder", 1000).show();
}
}
/**
* Creating location request object
* */
protected void createLocationRequest() {
try {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FATEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* Method to verify google play services on the device
* */
private boolean checkPlayServices() {
int resultCode = 0;
try {
resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "GooglePlayServicesUtil", 1000).show();
}
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Toast.makeText(getApplicationContext(),
"This device is not supported.", Toast.LENGTH_LONG)
.show();
finish();
}
return false;
}
return true;
}
/**
* Starting the location updates
* */
protected void startLocationUpdates() {
try {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* Stopping location updates
*/
protected void stopLocationUpdates() {
try {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
try {
mLastLocation = location;
// Toast.makeText(getApplicationContext(), "Location changed!",
// Toast.LENGTH_SHORT).show();
// Displaying the new location on UI
displayLocation();
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
try {
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
+ result.getErrorCode());
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
try {
displayLocation();
if (mRequestingLocationUpdates) {
startLocationUpdates();
}
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
try {
mGoogleApiClient.connect();
} catch (Exception e) {
// TODO: handle exception
}
}
}
&#13;
请告诉我我错在哪里。提前致谢。
答案 0 :(得分:0)
嘿,你可以查看这个回购
https://github.com/Keshava11/droidutility
并使用此LocationUtil类获取位置更新。
检查自述文件以了解LocationUtil的使用情况。 对于定期更新,您可以检查同一类中的 sendLocalLocationBroadcast()方法。
答案 1 :(得分:0)
我看到onLocationChanged(位置位置)导致的崩溃在某些设备上接收到null位置参数。
我将删除此方法中的catch-everything异常处理,而是检查onLocationChanged(位置位置)位置的null。
作为一般设计原则,您应该避免静默捕获所有异常,而是向用户报告特定错误或写入调试日志。然后,您将获得更多信息来追踪崩溃的原因。