2011-05-25 66 views
1

我是一個初學者在Android開發,所以如果這是顯而易見的原諒。我使用來自android.com的示例創建了一個簡單的web應用程序,但是當單擊tel:或mailto:等鏈接時,應用程序崩潰並且讓我強制退出。我想知道是否有人能告訴我爲什麼。Android:簡單的網絡應用程序崩潰的鏈接,然後http計劃

public class MyApplication extends Activity { 

    WebView mWebView; 

    private class InternalWebViewClient extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 

      // This is my web site, so do not override; let my WebView load the page 
      if (Uri.parse(url).getHost().equals("m.mydomain.nl")) { 
       return false; 
      } else { 
       // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs 
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
       startActivity(intent); 
       return true; 
      } 

     } 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { 
      mWebView.goBack(); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     /* Use the WebView class to display website */ 
     mWebView = (WebView) findViewById(R.id.webview); 
     mWebView.setWebViewClient(new InternalWebViewClient()); 
     mWebView.getSettings().setJavaScriptEnabled(true); 
     mWebView.loadUrl("http://m.mydomain.nl/"); 
    } 

} 

回答

3

對不起,這個實際上固定它。使用mailto時,沒有主機,因此getHost()將返回null並導致異常。

private class InternalWebViewClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 

     // This is my web site, so do not override; let my WebView load the page 
     if (Uri.parse(url).getHost() != null && Uri.parse(url).getHost().equals("m.domain.nl")) { 
      return false; 
     } else { 
      // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs 
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
      startActivity(intent); 
      return true; 
     } 

    } 
} 
1

Logcat中的錯誤是什麼? (您可以通過在Eclipse中打開DDMS視圖來檢查logcat,然後選擇正在運行應用程序的設備.Logcat應該以調試視角顯示,或者可以將其添加到主Java視圖中。)

相關問題