致命异常:java.lang.RuntimeException 无法销毁活动:java.lang.IllegalStateException:onSaveInstanceState后无法执行此操作
问题是三星SM G355H和micromax A106
这是我的代码
public class EnRouteFragment extends Fragment implements LocationListener{
View rootView;
LayoutInflater inflater;
ViewGroup container;
Context context;
private GoogleMap googleMap;
// flag for GPS status
boolean isGPSEnabled = false;
boolean canGetLocation = false;
// flag for network status
boolean isNetworkEnabled = false;
Location location; // location
Double dblIntLat=null; // latitude
Double dblIntLog=null; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 0; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
MarkerOptions dynamicMarker;
Marker markerCurre;
public EnRouteFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
// TODO Auto-generated method stub
this.inflater = inflater;
this.container = container;
initializeVariables();
initializeUIVariables();
initializeListeners();
attachEventListeners();
return rootView;
}
@Override
public void initializeVariables() {
// TODO Auto-generated method stub
context=getActivity();
if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.fragment_enroute_marchent, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
}
@Override
public void initializeUIVariables() {
// TODO Auto-generated method stub
}
@Override
public void initializeListeners() {
// TODO Auto-generated method stub
}
@Override
public void attachEventListeners() {
// TODO Auto-generated method stub
}
// ------------------------------------fetch lat long from GPS OR NETWORK provider-------
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
dblIntLat = location.getLatitude();
dblIntLog = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
dblIntLat = location.getLatitude();
dblIntLog = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
// -----------------------------------------end---------------
// --------------------------------------initialize google map---------------
private void initilizeMap() {
if (googleMap == null) {
System.out.println("--------- initilizeMap() if");
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.enroute_map)).getMap();
googleMap.setMyLocationEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.getUiSettings().setRotateGesturesEnabled(false);
googleMap.getUiSettings().setZoomControlsEnabled(true);
if(dblIntLat!=null && dblIntLog !=null)
{
markerCurre = googleMap.addMarker(new MarkerOptions().position(
new LatLng(dblIntLat, dblIntLog)).icon(BitmapDescriptorFactory.fromResource(R.drawable.bike)));
}
if(dblIntLat!=null && dblIntLog !=null)
{
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dblIntLat,dblIntLog) , 12.0f) );
}
if (googleMap == null) {
Toast.makeText(context,
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
else
{
System.out.println("--------- initilizeMap() else");
if(markerCurre!=null)
{
markerCurre.remove();
}
if(dblIntLat!=null && dblIntLog !=null)
{
markerCurre = googleMap.addMarker(new MarkerOptions().position(
new LatLng(dblIntLat, dblIntLog)).icon(BitmapDescriptorFactory.fromResource(R.drawable.bike)));
}
}
}
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
dblIntLat = loc.getLatitude();
dblIntLog = loc.getLongitude();
initilizeMap();
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
Fragment fragment = getFragmentManager().findFragmentById(R.id.enroute_map);
if(fragment!=null)
{
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commitAllowingStateLoss();
}
}
@Override
public void onPause() {
super.onPause();
if(locationManager!=null)
{
locationManager.removeUpdates(this);
}
}
@Override
public void onResume() {
super.onResume();
getLocation();
}
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
//super.onSaveInstanceState(outState);
}
}
这是错误日志cat
java.lang.RuntimeException: Unable to destroy activity : java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3733)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3751)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3951)
at android.app.ActivityThread.access$1000(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1323)
at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1341)
at android.app.BackStackRecord.commitInternal(BackStackRecord.java:597)
at android.app.BackStackRecord.commit(BackStackRecord.java:575)
at com.matrix.hungerz.EnRouteFragment.onDestroyView(EnRouteFragment.java:744)
at android.app.Fragment.performDestroyView(Fragment.java:1898)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:954)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
at android.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1887)
at android.app.Activity.performDestroy(Activity.java:5493)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1123)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3720)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3751)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3951)
at android.app.ActivityThread.access$1000(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(NativeStart.java)
答案 0 :(得分:0)
此logcat错误与您提供的代码不对应。
注意logcat中的这一行:
android.app.BackStackRecord.commit(BackStackRecord.java:575)
这意味着该错误是由致电FragmentTransaction.commit()
引起的,但您的代码建议您致电FragmentTransaction.commitAllowingStateLoss()
。
我可以想到三个可能的原因:
commit()
代替commitAllowingStateLoss()
,错误发生在旧版本的应用上AOSP代码的相关部分:
来自BackStackRecord
:
public int commit() {
return commitInternal(false);
}
public int commitAllowingStateLoss() {
return commitInternal(true);
}
int commitInternal(boolean allowStateLoss) {
if (mCommitted) {
throw new IllegalStateException("commit already called");
}
...
mManager.enqueueAction(this, allowStateLoss);
return mIndex;
}
来自FragmentManagerImpl
:
public void enqueueAction(Runnable action, boolean allowStateLoss) {
if (!allowStateLoss) {
checkStateLoss();
}
...
}
private void checkStateLoss() {
if (mStateSaved) {
throw new IllegalStateException(
"Can not perform this action after onSaveInstanceState");
}
if (mNoTransactionsBecause != null) {
throw new IllegalStateException(
"Can not perform this action inside of " + mNoTransactionsBecause);
}
}