【发布时间】:2019-07-25 09:42:39
【问题描述】:
我正在传递一个 Google 地图 URL 以使用地图应用程序启动搜索,并且过去一切正常。在我的 linux 机器上导入我的项目后,mapIntent.resolveActivity() 部分是 null,尽管设备有谷歌地图,gmINtentUri 是 nullhttps://www.google.com/maps/search/?api=1&query=Spilia%20Beach。我猜这是由于 URL 前面额外的 null 部分引起的。这是什么原因造成的?我的 Windows 机器上没有发生这种情况。
编辑:我发现字符串开头的gmIntentUri 的null 值是由于这个parcel.writeString(finalMapSearchUrl);。我稍后会提交答案。
@OnClick(R.id.show_in_map_button) void openMap() {
Uri gmIntentUri = Uri.parse(placeObject.getMapSearchUrl()); // Create a Uri from a string. Use the result to create an Intent
Intent mapIntent = new Intent(Intent.ACTION_VIEW,gmIntentUri);
// Make the Intent explicit by settings the Google Maps package
mapIntent.setPackage("com.google.android.apps.maps");
// Verify that there is an app available to receive the intent
if(mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent); // if the result is non-null there is at least one app that can handle the intent
} else {
Log.d(TAG,"Error resolving Activity for map Intent in openMap(), [Uri = " + gmIntentUri + "].");
Log.d(TAG,"mapIntent.resolveActivity() = " + mapIntent.resolveActivity(getPackageManager()));
}
}
这是包含 getMapSearchUrl() 的 PlaceObject.java 类:
public class PlaceObject implements Parcelable {
private static final String TAG = PlaceObject.class.getSimpleName();
// Using int so that the values can be accessed via R.string etc.
private int name;
private int description;
private String locationDistance;
private int category;
private static final String baseMapSearchUrl = "https://www.google.com/maps/search/?api=1&query="; // Base url for launching a Map activity with a Search Intent
private String finalMapSearchUrl = "";
PlaceObject(int name, int description, int category , String locationDistance, String mapUrlParam) {
this.name = name;
this.description = description;
this.locationDistance = locationDistance;
this.category = category;
finalMapSearchUrl += baseMapSearchUrl + Uri.encode(mapUrlParam);
Log.d(TAG,"Final map URL : " + finalMapSearchUrl);
}
private PlaceObject(Parcel in) {
name = in.readInt();
description = in.readInt();
locationDistance = in.readString();
category = in.readInt();
finalMapSearchUrl = in.readString();
}
public static final Creator<PlaceObject> CREATOR = new Creator<PlaceObject>() {
@Override
public PlaceObject createFromParcel(Parcel in) {
return new PlaceObject(in);
}
@Override
public PlaceObject[] newArray(int size) {
return new PlaceObject[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(name);
parcel.writeInt(description);
parcel.writeString(locationDistance);
parcel.writeInt(category);
parcel.writeString(finalMapSearchUrl);
}
public int getName() {
return name;
}
public int getDescription() {
return description;
}
public String getLocationDistance() {
return locationDistance;
}
public int getCategory() {
return category;
}
public String getMapSearchUrl() {
return finalMapSearchUrl;
}
}
P.S:PlaceObject()中的mapUrlParam是一个简单的String,根据谷歌的说法,由于空格等原因需要在使用前进行编码。
【问题讨论】:
-
请检查
placeObject.getMapSearchUrl()方法。也许null已经存在,应该修改而不是附加的代码部分。 -
@Blehi 谢谢,刚刚添加了那部分代码。老实说,我看不出如何在字符串的开头添加
null,它可能与Parcelable接口有关吗?我以前从未使用过它,所以我可能在构造函数中弄乱了一些东西。也许finalMapSearchUrl = in.readString()? -
您在日志中看到了什么:
Log.d(TAG,"Final map URL : " + finalMapSearchUrl);?也许在构造函数中而不是finalMapSearchUrl +=你应该使用finalMapSearchUrl = -
我已经在我的代码中应用了这两个更改,并发现
finalMapSearchUrl在构造函数中是可以的,但是当调用getMapSearchUrl()时,它是空的。关于价值在哪里丢失的任何想法? -
我认为最好的方法是逐步调试您的应用程序或在值可以更改的地方放置大量
Log.d()。抱歉,我没有其他选择。
标签: java android android-intent uri