Uri.parse() 在给定的 URL 之前添加 null



我正在传递一个谷歌地图网址,以使用地图应用程序启动搜索,一切都可以正常工作。在我的 linux 机器上导入我的项目后,尽管设备具有谷歌地图并且gmINtentUri nullhttps://www.google.com/maps/search/?api=1&query=Spilia%20Beach,但mapIntent.resolveActivity()部分null。我猜这是由于 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;
    }
}

PS:PlaceObject()中的mapUrlParam是一个简单的String,根据Google的说法,由于空格等原因,在使用之前需要对其进行编码。

我发现gmIntentUristring开始时的空值是由于这个parcel.writeString(finalMapSearchUrl); 至于评论中的问题,我会写一篇不同的帖子。

最新更新