使用带有片段的地图旋转设备后,“活动已被破坏”

时间:2015-11-18 08:04:13

标签: android android-fragments xamarin xamarin.android

尝试旋转设备后,我收到此异常:

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

1 个答案:

答案 0 :(得分:1)

尝试放

<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();
    }
}