PutParcelable 不能应用于 Place.Type 错误?
Posted
技术标签:
【中文标题】PutParcelable 不能应用于 Place.Type 错误?【英文标题】:PutParcelable cannot be applied to Place.Type error? 【发布时间】:2020-06-01 23:49:32 【问题描述】:我正在尝试将地图数据从活动上的地图片段传递到同一活动上的单独片段。
当用户点击地图 POI 时,我调用 google 并获取了一堆数据,包括我想传递给新片段的 Place.Type,不幸的是 Place.Type 返回一个错误,说是错误的类型..无法打包。 android Studio 为我提供了一个转换为 parcelable 的选项,我尝试过并崩溃了。
在参考中,谷歌说 Place.Types 实现了 parcelable 所以我有点困惑为什么会失败
我确信有一种方法可以循环思考和重铸或其他东西,但我缺乏知识让我退缩了,有人可以在这里帮忙吗?
public class ZombieMapActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationButtonClickListener, GoogleMap.OnMyLocationClickListener, GoogleMap.OnPoiClickListener
private GoogleMap mMap;
private final int PERM_REQUEST_LOCATION_INT = 1;
private FusedLocationProviderClient FusedLocationClient;
private String apiKey;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zombie_map);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//location
FusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
//places API stuff
apiKey = getResources().getString(R.string.google_maps_key);
Places.initialize(getApplicationContext(), apiKey);
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
//perm check
...
mapSetup();
private void requestPerms() ...
//catch request reply
@Override
public void onRequestPermissionsResult(
int requestCode,
String permissions[],
int[] grantResults) ...
private void mapSetup() ...
@Override
public boolean onMyLocationButtonClick() ...
@Override
public void onMyLocationClick(@NonNull Location location)
makeToast("This is you");
@Override
public void onPoiClick(final PointOfInterest poi)
//distance check and throw out if to far
//great place to think about a caching system for types.. to cut down on requests
//placeId to place type method
PlacesClient placesClient = Places.createClient(this);
String placeId = poi.placeId;
List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.TYPES);
FetchPlaceRequest request = FetchPlaceRequest.newInstance(placeId, placeFields);
placesClient.fetchPlace(request).addOnSuccessListener(new OnSuccessListener<FetchPlaceResponse>()
@Override
public void onSuccess(FetchPlaceResponse fetchPlaceResponse)
Place place = fetchPlaceResponse.getPlace();
List<Place.Type> types = place.getTypes();
String placeName = poi.name;
makeLootFragment(types,placeName);
makeToast(placeName);
);
private void makeLootFragment(List<Place.Type> _types,String name)
//pack it for sending
Bundle lootBundle = new Bundle();
lootBundle.putParcelable("bLootTypes" , (Parcelable) _types);//not working
lootBundle.putString("bName",name);
FragmentManager lootFragMan = getSupportFragmentManager();
FragmentTransaction LootTrans = lootFragMan.beginTransaction();
LootFragment lootFragment = new LootFragment();
lootFragment.setArguments(lootBundle);
LootTrans.add(R.id.lootContainer,lootFragment);
LootTrans.addToBackStack("LootStack");
LootTrans.commit();
private void makeToast(String words)
Toast.makeText(this, words, Toast.LENGTH_SHORT).show();
任何其他基于此处显示的糟糕编码的建议也将不胜感激:D
【问题讨论】:
【参考方案1】:您要发送的对象的类应实现 Parcelable 接口,如下所示 -
public class Student implements Parcelable
private Integer rollno;
private String name;
private Integer age;
protected Student(Parcel in)
age = in.readInt();
name = in.readString();
rollno = in.readInt();
public Student(Integer age, String name, Integer rollno)
this.age = age;
this.name = name;
this.rollno = rollno;
public static final Creator<Student> CREATOR = new Creator<Student>()
@Override
public Student createFromParcel(Parcel in)
return new Student(in);
@Override
public Student[] newArray(int size)
return new Student[size];
;
@Override
public int describeContents()
return 0;
public Integer getRollno()
return rollno;
public void setRollno(Integer rollno)
this.rollno = rollno;
public String getName()
return name;
public void setName(String name)
this.name = name;
public Integer getAge()
return age;
public void setAge(Integer age)
this.age = age;
public static Creator<Student> getCREATOR()
return CREATOR;
@Override
public void writeToParcel(Parcel parcel, int i)
parcel.writeInt(age);
parcel.writeString(name);
parcel.writeInt(rollno);
所以你应该做一个类并实现 Parcelable 并将 Place.Type 内容写入其成员,然后将其传递给其他 Fragment
【讨论】:
所以实现parcelable的事情对我来说很难做到。你很好地复制了一个我可以编写的类的构造函数,但我正在使用的类来自谷歌。真正的问题是为什么Place.Tytpe,根据文档已经实现了parcelable,需要贴到另一个类上才能使用?因为 IO 仍然对为什么 parcelable 类不可parcelable 感到困惑:D 我也有权利创建一个新类,以 1 对 1 的方式实现和传输数据吗? bundle.putParcelableArrayList() 这个方法你试过了吗【参考方案2】:所以最后我做了一个“影子类”来保存来自地点请求的所有数据
public class PlaceDataHolder implements Parcelable
private Place placeData;
public PlaceDataHolder(Place _place)
placeData = _place;
当我写完这个类时,它从谷歌 OnSuccessListener 中接收对象并将其放入一个持有者变量中,这样我就可以在所述类上实现 Parcelable...
Android 工作室随后收到了一堆抱怨,并自动修复导致所有这些美妙的额外代码
protected PlaceDataHolder(Parcel in)
placeData = in.readParcelable(Place.class.getClassLoader());
public static final Creator<PlaceDataHolder> CREATOR = new Creator<PlaceDataHolder>()
@Override
public PlaceDataHolder createFromParcel(Parcel in)
return new PlaceDataHolder(in);
@Override
public PlaceDataHolder[] newArray(int size)
return new PlaceDataHolder[size];
;
@Override
public int describeContents()
return 0;
@Override
public void writeToParcel(Parcel dest, int flags)
dest.writeParcelable(placeData, flags);
public List<Place.Type> getTypes()
return placeData.getTypes();
这最后一点是我在几乎为零的想法下生成的,只是悬停在红线上,然后按 alt+ 进入我的方式来获得这个光荣的工作解决方案
【讨论】:
以上是关于PutParcelable 不能应用于 Place.Type 错误?的主要内容,如果未能解决你的问题,请参考以下文章
更改 Google Place Picker Appbar 文本颜色