2013-07-26 105 views
1

我的應用程序中有一個ImageView,我希望它在單擊時打開某個網站。我很確定我做的一切正確,但仍然無法正常工作。我點擊ImageView後,應用程序崩潰。當我將一個onClickListener添加到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的文件:

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) 

回答

1

替換此

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

通過

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

更多信息@

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

編輯:

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

是的,這是問題!謝謝! – Guy

+0

@Matthew檢查你的url是否在它之前有'http'或'https'。 – Raghunandan

1

你還需要確保你的URL格式正確無誤(前綴是重要的​​):

http://www.google.com 

www.google.com 

檢查OU t this post爲例。

+1

這是行不通的,無論如何謝謝 – Guy

+0

'openUrl.addCategory(Intent.CATEGORY_BROWSABLE);'與崩潰無關。 http://developer.android.com/reference/android/content/Intent.html。向下滾動並檢查'CATEGORY_BROWSABLE'。 – Raghunandan

0

你想在瀏覽器窗口中打開網站?如果是這樣,請刪除CATEGORY。我非常確定這隻適用於WebViews。

相關問題