简体   繁体   English

将onClickListener添加到ImageView时,应用程序崩溃

[英]App crashes when I add an onClickListener to ImageView

I have an ImageView in my app and I want it to open a certain website when it is clicked. 我的应用程序中有一个ImageView,我希望它在单击时打开某个网站。 I'm pretty sure I'm doing everything right but it still won't work. 我敢肯定我做的一切正确,但仍然无法正常工作。 The app crashes as soon as I click the ImageView. 单击ImageView后,应用程序崩溃。

ImageView banner = (ImageView)findViewById(R.id.banner);
banner.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent openUrl = new Intent();
            openUrl.setAction(Intent.ACTION_VIEW);
            openUrl.addCategory(Intent.CATEGORY_BROWSABLE);
            openUrl.setData(Uri.parse("www.google.com"));
            startActivity(openUrl);
        }
    });

Logcat file: Logcat文件:

07-26 21:53:49.481: E/AndroidRuntime(25479): FATAL EXCEPTION: main
07-26 21:53:49.481: E/AndroidRuntime(25479): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=www.google.com }
07-26 21:53:49.481: E/AndroidRuntime(25479):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at android.app.Activity.startActivityForResult(Activity.java:3370)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at android.app.Activity.startActivityForResult(Activity.java:3331)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at android.app.Activity.startActivity(Activity.java:3566)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at android.app.Activity.startActivity(Activity.java:3534)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at si.dvanadva.evanturist.CheckpointsActivity$2.onClick(CheckpointsActivity.java:323)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at android.view.View.performClick(View.java:4204)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at android.view.View$PerformClick.run(View.java:17355)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at android.os.Handler.handleCallback(Handler.java:725)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at android.os.Looper.loop(Looper.java:137)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at android.app.ActivityThread.main(ActivityThread.java:5041)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at java.lang.reflect.Method.invokeNative(Native Method)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at java.lang.reflect.Method.invoke(Method.java:511)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-26 21:53:49.481: E/AndroidRuntime(25479):    at dalvik.system.NativeStart.main(Native Method)

You also need to ensure your URL is correctly formatted (prefix is important): 您还需要确保URL的格式正确(前缀很重要):

http://www.google.com

not

www.google.com

Check out this post for an example. 查看此帖子以获取示例。

Replace this 取代这个

  openUrl.setData(Uri.parse("www.google.com")); 

by 通过

  openUrl.setData(Uri.parse("http://www.google.com"));

More info @ 更多信息 @

http://developer.android.com/reference/android/content/Intent.html http://developer.android.com/reference/android/content/Intent.html

Edit: 编辑:

    String url = "www.google.com";
    if (!url.startsWith("http://") && !url.startsWith("https://"))
    {
         url = "http://" + url;
    }
    else
    {
    Intent openUrl = new Intent();
    openUrl.setAction(Intent.ACTION_VIEW);
    openUrl.addCategory(Intent.CATEGORY_BROWSABLE);
    openUrl.setData(Uri.parse(url));      
    startActivity(openUrl);
    }

You want to open the website in a browser window? 您要在浏览器窗口中打开网站吗? If so, remove the CATEGORY. 如果是这样,请删除类别。 I'm pretty sure that's only for WebViews. 我很确定这仅适用于WebView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM