我正在android中开发一个简单的跟踪应用程序。该应用程序在Android 4.1版本中运行良好。但是,当我尝试在Android 4.4版本设备中运行该应用程序时,该应用程序崩溃。我不知道为什么。请让我任何想法来解决我的问题。在这里,我附上了我的代码。
private GoogleMap googleMap;
private LocationManager locationManager;
private String provider;
Location location;
private Handler customHandler = new Handler();
String currentdatetime_tosend, latitude_tosend, logitude_tosend,
address_tosend, speed_tosend, username_tosend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
initilizeMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(false);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
}
private void AddMarker(double latitude, double longitude, String driverName) {
googleMap.clear();
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title(driverName);
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.driver_car));
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
public void onLocationChanged(Location location) {
Double lat = (Double) (location.getLatitude());
Double lng = (Double) (location.getLongitude());
speed_tosend = Float.toString(location.getSpeed());
latitude_tosend = lat.toString();
logitude_tosend = lng.toString();
getAddress();
new SendDetailsInBackground().execute();
}
private void BackgroundProcess() {
customHandler.postDelayed(updateTimerThread, 0);
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
customHandler.postDelayed(this, 30000);
}
};
private void StartTheProcess() {
BackgroundProcess();
}
private void StopGetingLocation() {
customHandler.removeCallbacks(updateTimerThread);
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
GetLocaion();
}
private void GetLocaion() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
} else {
if (location != null) {
onLocationChanged(location);
Toast.makeText(getApplicationContext(), "Location Activated",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
"No location found..!!", Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(), "GPS IS Ennabled..!!",
Toast.LENGTH_SHORT).show();
double latitude = (Double) (location.getLatitude());
double longitude = (Double) (location.getLongitude());
AddMarker(latitude, longitude, "nani");
}
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog,
final int id) {
startActivity(new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog,
final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.startGettingLocation) {
StartTheProcess();
return true;
}
if (id == R.id.stopGetingLocation) {
StopGetingLocation();
return true;
}
return super.onOptionsItemSelected(item);
}
private String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
private void getAddress() {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(
Double.parseDouble(latitude_tosend),
Double.parseDouble(logitude_tosend), 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i) + ",");
/*Toast.makeText(getApplicationContext(),
"\n Address==>: " + sb.toString(), Toast.LENGTH_LONG)
.show();*/
address_tosend = sb.toString();
} else {
Toast.makeText(getApplicationContext(), "Address is empty!",
Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "No Address returned!",
Toast.LENGTH_SHORT).show();
}
}
这是我的代码。当我尝试使用gps时,项目崩溃了。请查看我的代码,让我知道为什么我的项目崩溃了。在Android 4.1版本中,相同的代码工作正常。
Target name - Google APIsX86
Vendor - Google Inc
Platform - 4.4.2
Api Level - 19
请帮我解决我的问题。
我的Logcat:
11-29 12:45:23.490: E/AndroidRuntime(1710): FATAL EXCEPTION: main
11-29 12:45:23.490: E/AndroidRuntime(1710): Process: info.androidhive.googlemapsv2, PID: 1710
11-29 12:45:23.490: E/AndroidRuntime(1710): java.lang.RuntimeException: Unable to resume activity {info.androidhive.googlemapsv2/info.androidhive.googlemapsv2.MainActivity}: java.lang.NullPointerException
11-29 12:45:23.490: E/AndroidRuntime(1710): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
11-29 12:45:23.490: E/AndroidRuntime(1710): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
11-29 12:45:23.490: E/AndroidRuntime(1710): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
11-29 12:45:23.490: E/AndroidRuntime(1710): at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-29 12:45:23.490: E/AndroidRuntime(1710): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-29 12:45:23.490: E/AndroidRuntime(1710): at android.os.Handler.dispatchMessage(Handler.java:102)
11-29 12:45:23.490: E/AndroidRuntime(1710): at android.os.Looper.loop(Looper.java:136)
11-29 12:45:23.490: E/AndroidRuntime(1710): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-29 12:45:23.490: E/AndroidRuntime(1710): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 12:45:23.490: E/AndroidRuntime(1710): at java.lang.reflect.Method.invoke(Method.java:515)
11-29 12:45:23.490: E/AndroidRuntime(1710): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-29 12:45:23.490: E/AndroidRuntime(1710): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-29 12:45:23.490: E/AndroidRuntime(1710): at dalvik.system.NativeStart.main(Native Method)
11-29 12:45:23.490: E/AndroidRuntime(1710): Caused by: java.lang.NullPointerException
11-29 12:45:23.490: E/AndroidRuntime(1710): at info.androidhive.googlemapsv2.MainActivity.GetLocaion(MainActivity.java:159)
11-29 12:45:23.490: E/AndroidRuntime(1710): at info.androidhive.googlemapsv2.MainActivity.onResume(MainActivity.java:134)
11-29 12:45:23.490: E/AndroidRuntime(1710): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
11-29 12:45:23.490: E/AndroidRuntime(1710): at android.app.Activity.performResume(Activity.java:5310)
11-29 12:45:23.490: E/AndroidRuntime(1710): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
11-29 12:45:23.490: E/AndroidRuntime(1710): ... 12 more