gpt4 book ai didi

android - 在 Fragment 中使用 Google map 时应用崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 12:34:59 25 4
gpt4 key购买 nike

我是第一次在我的应用程序中使用 Fragments。我已经在其中一个 fragment 中实现了 Google map ,每当我选择具有 map 的选项卡时,它就会成功加载,但是一旦我在其他一些 Activity 中进入主菜单,它就会崩溃,我不知道为什么。

这是我的崩溃日志:-

03-28 14:14:23.175: E/AndroidRuntime(18215): FATAL EXCEPTION: main
03-28 14:14:23.175: E/AndroidRuntime(18215): java.lang.RuntimeException: Unable to destroy activity {MyFragmentActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3444)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3462)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.app.ActivityThread.access$1200(ActivityThread.java:149)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.os.Looper.loop(Looper.java:153)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.app.ActivityThread.main(ActivityThread.java:5000)
03-28 14:14:23.175: E/AndroidRuntime(18215): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 14:14:23.175: E/AndroidRuntime(18215): at java.lang.reflect.Method.invoke(Method.java:511)
03-28 14:14:23.175: E/AndroidRuntime(18215): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
03-28 14:14:23.175: E/AndroidRuntime(18215): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-28 14:14:23.175: E/AndroidRuntime(18215): at dalvik.system.NativeStart.main(Native Method)
03-28 14:14:23.175: E/AndroidRuntime(18215): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1361)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
03-28 14:14:23.175: E/AndroidRuntime(18215): at com.fragments.FindMyCar.onDestroyView(FindMyCar.java:297)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.support.v4.app.Fragment.performDestroyView(Fragment.java:1705)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:996)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1921)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:335)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.app.Activity.performDestroy(Activity.java:5184)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1111)
03-28 14:14:23.175: E/AndroidRuntime(18215): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3431)
03-28 14:14:23.175: E/AndroidRuntime(18215): ... 11 more

在我的 fragment 中,我是这样实现的:-

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.findmycar, null);

initViews(view);

// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());


if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getActivity(), requestCode);
dialog.show();

}else { // Google Play Services are available

fm = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);

map = fm.getMap();

if(map == null){

//Toast.makeText(getActivity(), "null", 0).show();
}
else{

map.setMyLocationEnabled(true);
// checking for the best provider and connectivity

// Getting LocationManager object from System Service LOCATION_SERVICE
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

if (netInfo !=null && netInfo.isConnected()) {


if(isGpsEnable()){


provider=LocationManager.GPS_PROVIDER;
mLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if(mLocation==null){
//onLocationChanged(location);

provider=LocationManager.NETWORK_PROVIDER;
mLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

Log.d("Location","Location " + mLocation + " " + provider);

if(mLocation!=null){

onLocationChanged(mLocation);
}
else{

Toast.makeText(getActivity(), "Unable to fetch your current location at the moment. Please try again later.", Toast.LENGTH_LONG).show();
}

}else {
onLocationChanged(mLocation);
}

}
else{

showGpsAlert();
}

}
else{

Toast.makeText(getActivity(), "No internet connection", Toast.LENGTH_LONG).show();
}



}
}


@Override
public void onDestroyView() {
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
if(fragment!=null){
Toast.makeText(getActivity(), "inif", 0).show();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
else{


}

}


@Override
public void onDestroy() {
super.onDestroy();
}

@Override
public void onResume() {
super.onResume();
}
}

它在 onDestroyView() 方法中销毁引用时完美运行,但在该应用程序崩溃后,不知道该怎么做。

任何帮助都将不胜感激。提前致谢。

最佳答案

试试这个,让我知道将以下代码放入 onDestroyView()

try{
SupportMapFragment fragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}catch(Exception e){
}

关于android - 在 Fragment 中使用 Google map 时应用崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22657606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com