尝试旋转设备后出现此异常:

java.lang.IllegalStateException: Activity has been destroyed.


在我的OnCreate()中,我做的很简单:

FragmentTransaction fragTx = FragmentManager.BeginTransaction();
_myMapFragment = MapFragment.NewInstance(mapOptions);
fragTx.Replace(Resource.Id.map, _myMapFragment, "map");
fragTx.Commit();


而且我也在尝试删除OnDestroy()中的mapfragment,如下所示:

FragmentTransaction fragTx = FragmentManager.BeginTransaction();
fragTx.Remove(_myMapFragment);
fragTx.CommitAllowingStateLoss();


在此先感谢您提供任何帮助。

这是我的Main.axml的样子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_below="@id/toolbar">
    <AutoCompleteTextView
        android:hint="Nazwa ulicy w Warszawie"
        android:id="@+id/txtTextSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:imeOptions="actionSearch"
        android:imeActionId="@+id/txtTextSearch"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp" />
</LinearLayout>
<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    class="com.google.android.gms.maps.MapFragment" />
<Button
    android:text="Moja lokalizacja"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:id="@+id/btnLocalizeMe"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@android:color/holo_orange_dark" />




和我的MainActivity的标题:

public class MainActivity : AppCompatActivity,
    GoogleApiClient.IConnectionCallbacks,
    GoogleApiClient.IOnConnectionFailedListener,
    ILocationListener

最佳答案

试穿

<activity
    android:name=".MainActivity"
    android:configChanges="orientation|screenSize" />


和活动中

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

07-27 17:16