当我退出应用程序时,我得到以下异常。我的应用包含一个导航抽屉。每个抽屉都是一个片段。我在一个片段中有一个地图,其他片段包含简单的布局。我在主要活动中处理了新闻并完成了那里的活动。但是当我从地图片段返回时,我的应用程序崩溃了(在地图片段中,我只是获取当前位置)。这是我得到的堆栈跟踪:
04-14 10:20:40.822: E/AndroidRuntime(17446): FATAL EXCEPTION: main
04-14 10:20:40.822: E/AndroidRuntime(17446): java.lang.NullPointerException
04-14 10:20:40.822: E/AndroidRuntime(17446): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1372)
04-14 10:20:40.822: E/AndroidRuntime(17446): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
04-14 10:20:40.822: E/AndroidRuntime(17446): at android.os.Handler.handleCallback(Handler.java:615)
04-14 10:20:40.822: E/AndroidRuntime(17446): at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 10:20:40.822: E/AndroidRuntime(17446): at android.os.Looper.loop(Looper.java:137)
04-14 10:20:40.822: E/AndroidRuntime(17446): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-14 10:20:40.822: E/AndroidRuntime(17446): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 10:20:40.822: E/AndroidRuntime(17446): at java.lang.reflect.Method.invoke(Method.java:511)
04-14 10:20:40.822: E/AndroidRuntime(17446): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-14 10:20:40.822: E/AndroidRuntime(17446): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-14 10:20:40.822: E/AndroidRuntime(17446): at dalvik.system.NativeStart.main(Native Method)
这是我的片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_photos, container, false);
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.currentmap)).getMap();
geocoder = new Geocoder(getActivity(), Locale.getDefault());
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getActivity()
.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) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
return rootView;
}
private String getCompleteAddressString(double LATITUDE, double LONGITUDE) {
String strAdd = "";
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE,
LONGITUDE, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress
.append(returnedAddress.getAddressLine(i)).append(
"\n");
}
strAdd = strReturnedAddress.toString();
tvLocation.setText(strAdd);
Log.w("My Current loction address",
"" + strReturnedAddress.toString());
} else {
Log.w("My Current loction address", "No Address returned!");
}
} catch (Exception e) {
e.printStackTrace();
Log.w("My Current loction address", "Canont get Address!");
}
return strAdd;
}
@Override
public void onLocationChanged(Location location) {
tvLocation = (TextView) rootView.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();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView tv_location
getCompleteAddressString(latitude, 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 void onDestroyView ()
{
try{
MapFragment fragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.currentmap));
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}catch(Exception e){
}
super.onDestroyView();
}*/
@Override
public void onDestroyView() {
super.onDestroyView();
Log.v("in on destroy", "destroy");
Fragment fragment = (getFragmentManager()
.findFragmentById(R.id.currentmap));
if (fragment != null) {
getActivity().getFragmentManager().beginTransaction()
.remove(fragment).commit();
Log.v("in on destroy", "destroy");
}
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.currentmap)).getMap();
}
}
这是什么解决方案?
答案 0 :(得分:0)
您需要销毁地图片段,并且onResume()
您需要再次初始化Google地图
public void onDestroyView()
{
super.onDestroyView();
Fragment fragment = getFragmentManager().findFragmentById(R.id.map);
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}