Google Maps intent returning error: No Activity found to handle Intent
Google Maps intent returning error: No Activity found to handle Intent
我有一个navigate
方法:
public void navigate(String coordinates){
Uri intentUri = Uri.parse("geo"+coordinates);
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setData(intentUri);
mapIntent.setPackage("com.google.android.apps.maps");
getContext().startActivity(mapIntent);
}
当我点击 TextView
:
时被调用
TextView location = (TextView) listItemView.findViewById(R.id.location);
location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
navigate(currentAttraction.getLocation());
}
});
但是,当应用程序 运行 并且我单击 TextView
时,应用程序 crashes and I get this error
:
2020-01-15 21:29:20.164 17438-17438/com.example.android.explorecapetown E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.explorecapetown, PID: 17438
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=geo-33.920864,18.418210 pkg=com.google.android.apps.maps }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2051)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1709)
at android.app.Activity.startActivityForResult(Activity.java:5192)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
at android.app.Activity.startActivityForResult(Activity.java:5150)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
at android.app.Activity.startActivity(Activity.java:5521)
at android.app.Activity.startActivity(Activity.java:5489)
at com.example.android.explorecapetown.AttractionAdapter.navigate(AttractionAdapter.java:72)
at com.example.android.explorecapetown.AttractionAdapter.onClick(AttractionAdapter.java:46)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access00(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
设备有 Google 地图。
我对使用地图意图还很陌生,所以我希望有人能告诉我我做错了什么。
P.S.
请注意,这发生在适配器中 class:
public class AttractionAdapter extends ArrayAdapter<Attraction> {
public AttractionAdapter(Context context, ArrayList<Attraction> attractions) {
super(context, 0, attractions);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
final Attraction currentAttraction = getItem(position);
ImageView imageView = (ImageView) listItemView.findViewById(R.id.attraction_image);
imageView.setImageResource(currentAttraction.getImageResourceId());
TextView attractionName = (TextView) listItemView.findViewById(R.id.attraction_name);
attractionName.setText(currentAttraction.getName());
TextView location = (TextView) listItemView.findViewById(R.id.location);
location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
navigate(currentAttraction.getLocation());
}
});
// Find the ImageView in the list_item.xml layout with the ID image.
TextView contact = (TextView) listItemView.findViewById(R.id.contact);
// Check if an image is provided for this word or not
if (currentAttraction.hasContact()) {
// If an image is available, display the provided image based on the resource ID
contact.setText(currentAttraction.getContact());
// Make sure the view is visible
contact.setVisibility(View.VISIBLE);
} else {
// Otherwise hide the ImageView (set visibility to GONE)
contact.setVisibility(View.GONE);
}
return listItemView;
}
public void navigate(String coordinates){
Uri intentUri = Uri.parse("geo"+coordinates);
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setData(intentUri);
mapIntent.setPackage("com.google.android.apps.maps");
getContext().startActivity(mapIntent);
}
}
你忘记了 geo 后面的冒号:
Uri intentUri = Uri.parse("geo:"+coordinates);
这应该是它找不到 activity 关联的原因。
使用 geo: 意图在指定位置和缩放级别显示地图的格式:
geo:latitude,longitude?z=zoom
并且缩放是可选的。
可在此处找到文档:https://developers.google.com/maps/documentation/urls/android-intents
这就是崩溃的原因。而且,作为额外的,你应该检查是否有任何 activity 来处理意图。因为其他设备无法安装地图应用程序。
public void navigate(上下文上下文,字符串坐标){
Uri intentUri = Uri.parse("geo:"+coordinates);
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setData(intentUri);
mapIntent.setPackage("com.google.android.apps.maps");
context.startActivity(mapIntent);
}
导航(getContext,currentAttraction.getLocation());
在触发任何隐式意图之前检查是否有任何 activity 处理意图总是安全的。
Caution: It's possible that a user won't have any apps that handle the implicit intent you send to startActivity(). Or, an app may be
inaccessible because of profile restrictions or settings put into
place by an administrator. If that happens, the call fails and your
app crashes. To verify that an activity will receive the intent, call
resolveActivity() on your Intent object. If the result is non-null,
there is at least one app that can handle the intent and it's safe to
call startActivity(). If the result is null, do not use the intent
//Uri intentUri = Uri.parse("geo:37.7749,-122.4194");
Uri intentUri = Uri.parse("geo:"+coordinates);
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setData(intentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(mapIntent);
} else {
//Show info
}
查看官方文档here
我有一个navigate
方法:
public void navigate(String coordinates){
Uri intentUri = Uri.parse("geo"+coordinates);
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setData(intentUri);
mapIntent.setPackage("com.google.android.apps.maps");
getContext().startActivity(mapIntent);
}
当我点击 TextView
:
TextView location = (TextView) listItemView.findViewById(R.id.location);
location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
navigate(currentAttraction.getLocation());
}
});
但是,当应用程序 运行 并且我单击 TextView
时,应用程序 crashes and I get this error
:
2020-01-15 21:29:20.164 17438-17438/com.example.android.explorecapetown E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.explorecapetown, PID: 17438
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=geo-33.920864,18.418210 pkg=com.google.android.apps.maps }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2051)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1709)
at android.app.Activity.startActivityForResult(Activity.java:5192)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
at android.app.Activity.startActivityForResult(Activity.java:5150)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
at android.app.Activity.startActivity(Activity.java:5521)
at android.app.Activity.startActivity(Activity.java:5489)
at com.example.android.explorecapetown.AttractionAdapter.navigate(AttractionAdapter.java:72)
at com.example.android.explorecapetown.AttractionAdapter.onClick(AttractionAdapter.java:46)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access00(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
设备有 Google 地图。
我对使用地图意图还很陌生,所以我希望有人能告诉我我做错了什么。
P.S.
请注意,这发生在适配器中 class:
public class AttractionAdapter extends ArrayAdapter<Attraction> {
public AttractionAdapter(Context context, ArrayList<Attraction> attractions) {
super(context, 0, attractions);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
final Attraction currentAttraction = getItem(position);
ImageView imageView = (ImageView) listItemView.findViewById(R.id.attraction_image);
imageView.setImageResource(currentAttraction.getImageResourceId());
TextView attractionName = (TextView) listItemView.findViewById(R.id.attraction_name);
attractionName.setText(currentAttraction.getName());
TextView location = (TextView) listItemView.findViewById(R.id.location);
location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
navigate(currentAttraction.getLocation());
}
});
// Find the ImageView in the list_item.xml layout with the ID image.
TextView contact = (TextView) listItemView.findViewById(R.id.contact);
// Check if an image is provided for this word or not
if (currentAttraction.hasContact()) {
// If an image is available, display the provided image based on the resource ID
contact.setText(currentAttraction.getContact());
// Make sure the view is visible
contact.setVisibility(View.VISIBLE);
} else {
// Otherwise hide the ImageView (set visibility to GONE)
contact.setVisibility(View.GONE);
}
return listItemView;
}
public void navigate(String coordinates){
Uri intentUri = Uri.parse("geo"+coordinates);
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setData(intentUri);
mapIntent.setPackage("com.google.android.apps.maps");
getContext().startActivity(mapIntent);
}
}
你忘记了 geo 后面的冒号:
Uri intentUri = Uri.parse("geo:"+coordinates);
这应该是它找不到 activity 关联的原因。
使用 geo: 意图在指定位置和缩放级别显示地图的格式:
geo:latitude,longitude?z=zoom
并且缩放是可选的。
可在此处找到文档:https://developers.google.com/maps/documentation/urls/android-intents
这就是崩溃的原因。而且,作为额外的,你应该检查是否有任何 activity 来处理意图。因为其他设备无法安装地图应用程序。
public void navigate(上下文上下文,字符串坐标){
Uri intentUri = Uri.parse("geo:"+coordinates);
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setData(intentUri);
mapIntent.setPackage("com.google.android.apps.maps");
context.startActivity(mapIntent);
}
导航(getContext,currentAttraction.getLocation());
在触发任何隐式意图之前检查是否有任何 activity 处理意图总是安全的。
Caution: It's possible that a user won't have any apps that handle the implicit intent you send to startActivity(). Or, an app may be inaccessible because of profile restrictions or settings put into place by an administrator. If that happens, the call fails and your app crashes. To verify that an activity will receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, do not use the intent
//Uri intentUri = Uri.parse("geo:37.7749,-122.4194");
Uri intentUri = Uri.parse("geo:"+coordinates);
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setData(intentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(mapIntent);
} else {
//Show info
}
查看官方文档here