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

Uri.parse() adds null before the given Url

我正在传递 Google 地图 Url 以使用地图应用启动搜索,一切正常。在我的 linux 机器上导入我的项目后,mapIntent.resolveActivity() 部分是 null,尽管设备有 google 个地图并且 gmINtentUrinullhttps://www.google.com/maps/search/?api=1&query=Spilia%20Beach。我猜这是由于 Url 前面的额外 null 部分造成的。是什么原因造成的?我的 Windows 机器上没有发生这种情况。

编辑:我发现字符串开头的 gmIntentUrinull 值是由于这个 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 class :

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,根据Google,由于空格需要在使用前进行编码等等

我发现 gmIntentUristring 开头的空值是由于这个 parcel.writeString(finalMapSearchUrl); 至于评论中的问题,我会改变一下 post。