我创建了一个应用程序来推广餐馆,刷卡视图和列表视图以及地图,但是关闭了他崩溃的应用程序,有人将错误发送给开发者控制台:
java.lang.RuntimeException: Unable to destroy activity {com.sapires.Pizza/com.sapires.Joes.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3517)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3535)
at android.app.ActivityThread.access$1400(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1259)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5144)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1360)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1378)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
at com.sapires.Joes.Mapa.onDestroyView(Mapa.java:70)
at android.support.v4.app.Fragment.performDestroyView(Fragment.java:1709)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1011)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)
at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1938)
at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:336)
at android.app.Activity.performDestroy(Activity.java:5403)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1117)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3504)
然后我去了地图并再次转到另一个屏幕,旋转屏幕甚至崩溃,出现此错误:
java.lang.RuntimeException: Unable to destroy activity {com.sapires.Pizza/com.sapires.Joes.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3795)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3813)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4013)
at android.app.ActivityThread.access$1000(ActivityThread.java:173)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5579)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1360)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1378)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
at com.sapires.Joes.Mapa.onDestroyView(Mapa.java:70)
at android.support.v4.app.Fragment.performDestroyView(Fragment.java:1709)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1011)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)
at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1938)
at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:336)
at android.app.Activity.performDestroy(Activity.java:5623)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1123)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3782)
我使用fragmentes。有人能解释一下会发生什么吗?
答案 0 :(得分:0)
try following on back press of map to another screen and use saveinstant state for rotation of screen
@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment f = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
答案 1 :(得分:0)
这是地图java
package com.sapires.Joes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.sapires.Pizza.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Mapa extends Fragment {
private static final double MARKER_LATITUDE = 42.027325;
private static final double MARKER_LONGITUDE = -8.640842;
GoogleMap map;
private FragmentActivity myContext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.mapa_3, container, false);
final LatLng position = new LatLng(MARKER_LATITUDE, MARKER_LONGITUDE);
// marker
FragmentManager fragManager = myContext.getSupportFragmentManager();
map = ((SupportMapFragment) fragManager.findFragmentById(R.id.mapFragment1)).getMap();
map.addMarker(new MarkerOptions()
.position(position)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pizzaria))
.title("Pizzaria Joes\n251 824 818"));
// camera position
map.setOnCameraChangeListener(new OnCameraChangeListener()
{
@Override
public void onCameraChange(CameraPosition arg0)
{
CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(position, 17);
map.animateCamera(cu);
map.setOnCameraChangeListener(null);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
});
return android;
}
@Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
@Override
public void onDestroyView() {
super.onDestroyView();
Fragment f = getFragmentManager().findFragmentById(R.id.mapFragment1);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
}
答案 2 :(得分:0)
试试这个
package com.sunil.assignment;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.sunil.assignment.R.drawable;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class First extends Fragment{
FragmentManager man;
private GoogleMap googleMap;
MapFragment mMapFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v=inflater.inflate(R.layout.firstscreen, container, false);
try {
// Loading map
initilizeMap();
} catch (Exception e)
{
e.printStackTrace();
}
LatLng mumbai=new LatLng(18.9300, 72.8200);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mumbai, 13));
// googleMap.addMarker(new MarkerOptions().title("Mumbai").snippet("Dream City").position(mumbai));
// latitude and longitude
double latitude = 18.9300;
double longitude = 72.8200;
// create marker
final MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello");
// adding marker
googleMap.addMarker(marker);
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "Churchgate", Toast.LENGTH_SHORT).show();
googleMap.addMarker( marker.
icon(BitmapDescriptorFactory.fromResource(R.drawable.images)).title("Mumbai").snippet("Dream City").
position(new LatLng(18.9300,72.8200)));
return true;
}
});
return v;
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
/*
mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
if (mMapFragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mMapFragment = MapFragment.newInstance();
fragmentTransaction.replace(R.id.map, mMapFragment).commit();
Toast.makeText(getActivity(), "null", Toast.LENGTH_SHORT).show();
}
if (mMapFragment != null)
{
googleMap = mMapFragment.getMap();
Toast.makeText(getActivity(), "hi", Toast.LENGTH_SHORT).show();
if (googleMap != null)
googleMap.setOnMapClickListener(new GoogleMap. OnMapClickListener()
{
@Override
public void onMapClick(LatLng point)
{
//TODO: your onclick stuffs
Toast.makeText(getActivity(), "hi", Toast.LENGTH_SHORT).show();
}
});
} */
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment f = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
}