2011-06-25 3 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> 

ダイアログは明示的なIntent(明示的にstartActivityを明示的に呼び出さない)ではなくAlertDialog.Builderで構築されているため、例外メッセージ状態としてフラグを追加することはできません。私は同じような例外を持ついくつかの記事を見つけましたが、誰も同じではなかった、または私の問題を解決した修正がありました。あなたの助けを前にありがとう。

答えて

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

関連する問題