gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:36:51 26 4
gpt4 key购买 nike

我正在传递 Google map 网址以使用 map 应用程序启动搜索,并且一切正常。在我的 Linux 机器上导入我的项目后,mapIntent.resolveActivity()部分是 null尽管该设备有谷歌地图和 gmINtentUrinullhttps://www.google.com/maps/search/?api=1&query=Spilia%20Beach 。我猜这是由于额外的 null 造成的Url 前面的部分。这是什么原因造成的?我的 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 类:

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;
}
}

附:mapUrlParamPlaceObject()很简单String根据 Google 的说法,由于空格等原因,需要在使用之前进行编码。

最佳答案

我发现gmIntentUristring开头的空值是由于这个parcel.writeString(finalMapSearchUrl); 至于评论中的问题,我会另外发一篇文章来讨论它。

关于java - Uri.parse() 在给定的 Url 之前添加 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57198929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com