【问题标题】:"Activity has been destroyed" after device rotation using map with fragment使用带有片段的地图旋转设备后“活动已被破坏”
【发布时间】:2015-11-18 18:42:00
【问题描述】:

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

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

【问题讨论】:

  • 看看这个post
  • 请更新完整的跟踪日志,有助于追踪您遇到的问题?

标签: android android-fragments xamarin xamarin.android


【解决方案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();
    }
}

【讨论】:

  • 那么你只需要减小图像的大小。它超出了堆内存。那就是错误来了
  • 不推荐使用此标志:stackoverflow.com/questions/27396892/… 我使用的不是图片而是谷歌地图
  • 检查一下它可能有用stackoverflow.com/questions/14800192/…
  • 此链接由@DmitryArc 发布在上面;)
  • 如果有效,请编辑评论 不,它没有帮助 – Tomasz Kowalczyk 40 分钟前
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
  • 1970-01-01
  • 2015-01-29
相关资源
最近更新 更多