【问题标题】:Google Maps intent returning error: No Activity found to handle Intent谷歌地图意图返回错误:没有找到处理意图的活动
【发布时间】:2020-05-02 15:16:30
【问题描述】:

我有一个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$1.onClick(AttractionAdapter.java:46)
        at android.view.View.performClick(View.java:7125)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access$3500(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 地图。

我对使用 Maps Intent 非常陌生,所以我希望有人能告诉我我做错了什么。

附言

注意这发生在适配器类中:

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

【问题讨论】:

    标签: android google-maps android-studio android-intent textview


    【解决方案1】:

    在触发任何隐式意图之前检查是否有任何活动来处理意图总是安全的。

    警告:用户可能没有任何应用来处理您发送到 startActivity() 的隐式意图。或者,一个应用程序可能是 由于配置文件限制或设置而无法访问 由管理员放置。如果发生这种情况,呼叫将失败,您的 应用程序崩溃。要验证活动是否会收到意图,请调用 resolveActivity() 在您的 Intent 对象上。如果结果非空, 至少有一个可以处理意图的应用程序,并且可以安全地 调用 startActivity()。如果结果为空,则不要使用意图

    //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

    【讨论】:

    • 谢谢。意图现在是打开谷歌地图,但不是显示位置,而是显示一个空白屏幕,顶部有搜索栏,没有地图。
    • 您能在此处添加您的坐标是什么样的吗?您还可以尝试使用 geo:37.7749,-122.4194 之类的示例数据吗?
    • 它现在似乎可以工作了,我真的不确定发生了什么变化。可能是设备正在播放。感谢您的帮助。
    【解决方案2】:

    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());

    【讨论】:

      【解决方案3】:

      geo 后面的冒号忘记了:

      Uri intentUri = Uri.parse("geo:"+coordinates);
      

      这应该是它找不到相关活动的原因。

      使用地理的格式:意图在指定位置和缩放级别显示地图:

      geo:latitude,longitude?z=zoom

      缩放是可选的。

      文档可以在这里找到:https://developers.google.com/maps/documentation/urls/android-intents

      这就是崩溃的原因。而且,作为额外的,您应该检查是否有任何活动来处理意图。因为其他设备无法安装地图应用。

      【讨论】:

        猜你喜欢
        • 2018-02-09
        • 1970-01-01
        • 2015-12-03
        • 1970-01-01
        • 2011-08-18
        • 2013-08-24
        • 2022-11-20
        • 1970-01-01
        相关资源
        最近更新 更多