我们已经使用google map API v2实现了google地图并使用片段活动,我的代码如下所示。 但是当我打开地图时,当我点击设备上的主页按钮时,我的应用程序崩溃了。请任何人帮助我。
这是我的班级。
public class MapDirectionActivity extends FragmentActivity implements
LocationListener {
GoogleMap googleMap;
String lat, lang, movieshowtimeidtrack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
if (Global.TrackTransactionId != null) {
Global.TrackTransactionId = "";
}
if (Global.movieshowtimeidtrack != null) {
Global.movieshowtimeidtrack = "";
}
lat = getIntent().getStringExtra("PLat").toString();// 26.119417
lang = getIntent().getStringExtra("PLang").toString();// -80.144931
movieshowtimeidtrack = getIntent().getStringExtra(
"movieshowtimeidtrack");
Global.movieshowtimeidtrack = movieshowtimeidtrack;
ApplicationTrack appTrack = new ApplicationTrack(
MapDirectionActivity.this);
appTrack.TrackAppPost("12");
Log.v("log_tag", "Lat: " + lat);
// Getting Google Play availability status
int status = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if (status != ConnectionResult.SUCCESS) { // Google Play Services are
// not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
requestCode);
dialog.show();
} else { // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service
// LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if (location == null) {
Criteria criteria1 = new Criteria();
criteria1.setAccuracy(Criteria.ACCURACY_COARSE);
// Finds a provider that matches the criteria
String provider1 = locationManager.getBestProvider(criteria1,
true);
// Use the provider to get the last known location
location = locationManager.getLastKnownLocation(provider1);
if (location != null) {
onLocationChanged(location);
}
}
else if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
}
@Override
public void onLocationChanged(Location location) {
// TextView tvLocation = (TextView) findViewById(R.id.tv_location);
/*
* // Getting latitude of the current location double latitude =
* location.getLatitude();
*
* // Getting longitude of the current location double longitude =
* location.getLongitude();
*/
double latitude = Double.valueOf(lat);
double longitude = Double.valueOf(lang);
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(latLng)
.title(Global.MapTheaterName).snippet(Global.MapAddress));// +Global.MapAddress));
// Showing the current location in Google Map
// .title(intent.getStringExtra("NAME") + "\n" +
// intent.getStringExtra("ADD")));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView tv_location
// tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude
// );
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是我的活动。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
如果有人的话,请详细说明。
这是我的logcat错误
12-19 15:19:33.089: E/AndroidRuntime(24942): FATAL EXCEPTION: main
12-19 15:19:33.089: E/AndroidRuntime(24942): java.lang.NoSuchMethodError: android.os.Bundle.getClassLoader
12-19 15:19:33.089: E/AndroidRuntime(24942): at com.google.android.gms.maps.SupportMapFragment.onSaveInstanceState(Unknown Source)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.support.v4.app.Fragment.performSaveInstanceState(Fragment.java:1607)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1587)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1655)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:527)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.app.Activity.performSaveInstanceState(Activity.java:1084)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1293)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2703)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2678)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2651)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.app.ActivityThread.access$1700(ActivityThread.java:132)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1045)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.os.Looper.loop(Looper.java:150)
12-19 15:19:33.089: E/AndroidRuntime(24942): at android.app.ActivityThread.main(ActivityThread.java:4263)
12-19 15:19:33.089: E/AndroidRuntime(24942): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 15:19:33.089: E/AndroidRuntime(24942): at java.lang.reflect.Method.invoke(Method.java:507)
12-19 15:19:33.089: E/AndroidRuntime(24942): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-19 15:19:33.089: E/AndroidRuntime(24942): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-19 15:19:33.089: E/AndroidRuntime(24942): at dalvik.system.NativeStart.main(Native Method)