2011-06-25 16 views
0

我的主要活動檢查是否是第一次用戶在其onCreate方法中運行應用程序。在對話框中,這是一個WebView,有一個超鏈接用於發送電子郵件給我。當用戶點擊鏈接,不過,我發現了以下異常:AndroidRuntimeException當在第一次運行時顯示的對話框中單擊超鏈接

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 
     at android.app.ContextImpl.startActivity(ContextImpl.java:651) 
     at android.content.ContextWrapper.startActivity(ContextWrapper.java:258) 
     at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:235) 
     at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:330) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:143) 
     at android.app.ActivityThread.main(ActivityThread.java:4717) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:521) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
     at dalvik.system.NativeStart.main(Native Method) 

導致異常的活動是在清單文件中設置這樣的:

<activity android:name=".MainActivity" 
       android:label="@string/app_name"> 
     <intent android:action="android.intent.action.VIEW" /> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

這是

private Dialog createFirstRunDialog() { 
    LayoutInflater inflator = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
    View layout = inflator.inflate(R.layout.tutorial, (ViewGroup)findViewById(R.id.tutorialMain)); 

    WebView webView = (WebView)layout.findViewById(R.id.tutorialBody); 
    webView.loadUrl(TUTORIAL_HTML_FILE); 

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
    dialogBuilder.setView(layout) 
     .setCancelable(true) 
     .setIcon(R.drawable.icon) 
     .setTitle(R.string.tutorial_title) 
     .setNegativeButton(R.string.tutorial_doneButton, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dismissDialog(TUTORIAL_DIALOG_ID); 
      } 
     }); 
    return dialogBuilder.create(); 
} 

在HTML中的鏈路(資產內部)是一個典型的mailto連結:

創建該對話框代碼
<a href="mailto:[email protected]?subject=MyApp feature request">Tell us</a> 

由於該對話框是用AlertDialog.Builder構建的,而不是顯式的Intent(並不實際調用startActivity),所以我無法[據我所知]添加該標誌作爲異常消息狀態。我在這裏發現了一些類似的異常帖子,但沒有一個是相同的,或者有解決我的問題的修復程序。在此先感謝您的幫助。

回答

1

你必須攔截鏈接並自己處理。

WebView webView = (WebView)layout.findViewById(R.id.tutorialBody); 
webView.setWebViewClient(new WebViewClient() { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (url.startsWith("mailto:")) { 
      MailTo mt = MailTo.parse(url); 
      Intent intent = new Intent(); 
      intent.setType("text/html"); 
      intent.putExtra(Intent.EXTRA_EMAIL, new String[] {mt.getTo()}); 
      intent.putExtra(Intent.EXTRA_SUBJECT, mt.getSubject()); 
      startActivity(Intent.createChooser(intent, "Email ...")); 
      return true; 
     } 
     return false; 
    } 
}); 
webView.loadUrl(TUTORIAL_HTML_FILE); 
+0

謝謝 - 這是訣竅! – areyling

相關問題