【问题标题】:Is GoogleMap inherently Parcelable?GoogleMap 本质上是 Parcelable 吗?
【发布时间】:2018-02-27 06:19:23
【问题描述】:

我对 java 和 android 很陌生。我被一个基本问题困住了。

在这个给定的fragment 中,我可以将GoogleMap 对象添加为parcelable,而无需任何额外的pracelable 类:

    public class SecondFragment extends Fragment
            implements OnMapReadyCallback {

      public GoogleMap mMap;
      @Override
        public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);                      
          if (savedInstanceState != null) {
            mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
            mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
          }
        }
 @Override
  public void onSaveInstanceState(Bundle outState) {
    if (mMap != null) {
      outState.putParcelable(KEY_CAMERA_POSITION, mMap.getCameraPosition());
      outState.putParcelable(KEY_LOCATION, mLastKnownLocation);
      super.onSaveInstanceState(outState);
    }
  }

这是我第一次遇到这种可打包的方法,所以,我在另一个类中尝试了相同的字符串:

public String location;//="No location name found";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Retrieve location and camera position from saved instance state.
    if (savedInstanceState != null) {
      location = savedInstanceState.getParcelable(KEY_LOCATION_NAME);          
    }
  }

   @Override
  public void onSaveInstanceState(Bundle outState) {
    if (location != null) {
      outState.putParcelable(KEY_LOCATION_NAME, location);
      super.onSaveInstanceState(outState);
    }
  }

这显示编译时错误:

Error:(205, 49) error: incompatible types: String cannot be converted to Parcelable

所以,我的问题是为什么同样的事情适用于 googleMap 而不是字符串? googleMap 是否天生可打包,而对于“普通”字符串,我们必须创建一个可打包类?

【问题讨论】:

    标签: android parcelable


    【解决方案1】:

    您可以阅读GoogleMap 课程页面,看看它不是 implements Parcleable

    同样的事情也适用于 googleMap

    您没有保存地图...您正在保存CameraPosition

    location 是字符串,而不是 Google 地图。

    如果您想保存位置LatLngLocation Parcelable

    对于“普通”字符串,我们必须创建一个parcelable类吗?

    不。使用字符串,只需使用outState.putString()

    【讨论】:

    • 感谢您的回复。两件事情。 1.我以为我也在保存位置(KEY_Location)。 2. 你的意思是在我的onSaveInstanceState 上使用outState.putExtra()?这是给cant solve method 错误。
    • 我猜mLastKnownLocation 是一个 Location 对象,而不是一个字符串......你应该保持你的类型相同。
    • 非常感谢。就在不久的将来,我可以为字符串数组做putCharArraySequence 吗?
    • 没有。 CharSequenceString,一个字符数组。不是字符串数组。随意阅读文档以了解您可以使用的内容。 developer.android.com/reference/android/os/Bundle.html
    • 再次抱歉...读错了。 putCharSequenceArray 将接受一个字符串数组,是的
    【解决方案2】:

    Parcelable 用于复杂模型。

    如果你想将模型转换为 parcelable,你应该实现 Parcelable 。 在 Android Studio 中有一些插件可以为您完成所有这些艰巨的工作。 在您的示例中,请注意在意图中传递对象或将它们保存到实例以供以后使用,每种类型都有不同的方法。

    putString for string
    putInt for Int
    ....
    

    对于您想要的类型,您应该使用适当的方法

    【讨论】:

    【解决方案3】:

    String 是基本类型,因此不应将其用于 Parcelable。

    如果您想将 Parcelable 用于类对象。该类应该已经实现了 Parcelable。

    【讨论】:

      猜你喜欢
      • 2010-12-31
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 2013-08-03
      相关资源
      最近更新 更多