问题描述
我对 java 和 android 很陌生.我被一个基本问题困住了.
I am very new to java and android. I am stuck with a basic problem.
在这个给定的 fragment
中,我可以将 GoogleMap
对象添加为 parcelable
而不需要任何额外的 pracelable
类:
In this given fragment
, I can add GoogleMap
object as parcelable
without any extra pracelable
class as:
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);
}
}
这是我第一次遇到这种可分割的方法,所以,我在另一个类中尝试了相同的字符串:
This is my first encounter with this parcelable approach, so, I tried the same in another class for a string:
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 本身是可分块的,对于普通"字符串,我们必须创建一个可分块的类?
So, my question is why the same thing is working for googleMap and not for a string? Is googleMap inherently parcelable, and for "normal" String, we must create a parcelable class?
推荐答案
您可以阅读GoogleMap
类页面以查看它没有 实现 Parcleable
.
同样的事情适用于 googleMap
您不是在保存地图...您是在保存 CameraPosition
You are not saving the map... You are saving the CameraPosition
location
是一个字符串,而不是谷歌地图.
location
is a String, not a Google Map.
如果您想保存位置,LatLng
或 Location
is Parcelable
.
对于普通"字符串,我们必须创建一个parcelable类?
没有.对于字符串,只需使用 outState.putString()
Nope. With a string, just use outState.putString()
这篇关于GoogleMap 本身是可包裹的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!