[英]Uri.parse() adds null before the given Url
我通過了Google Maps Url,以使用Maps應用啟動搜索,並且一切正常。 在我的Linux機器上導入我的項目后,盡管該設備具有google maps並且gmINtentUri
為null
但mapIntent.resolveActivity()
部分為null
nullhttps://www.google.com/maps/search/?api=1&query=Spilia%20Beach
。 我猜這是由於網址前面多余的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;
}
}
PS: mapUrlParam
PlaceObject()
中的PlaceObject()
是一個簡單的String
,根據Google的說法,由於空格等原因,在使用前需要對其進行編碼。
我發現string
開頭的gmIntentUri
的null值是由於這個parcel.writeString(finalMapSearchUrl);
至於評論中的問題,我將對此發表另一篇文章。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.