2012-12-25 70 views
2

我確實有一個應用程序幾乎沒有屏幕(活動)。從其中的一個,這是支持屏幕,我必須能夠運行電子郵件,共享和其他活動。所以,我已經添加了發送郵件選項以這樣的方式錯誤:從活動外部調用活動

case R.id.firstColumn: 
      /* Create the Intent */ 
      final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 

      /* Fill it with Data */ 
      emailIntent.setType("plain/text"); 
      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Feedback from customers"); 


      /* Send it off to the Activity-Chooser */ 
      context.startActivity(Intent.createChooser(emailIntent, "Send mail...")); 
      break; 

我建議將運行默認的內置電子郵件應用程序,將給予用戶選擇填寫的內容,然後發送電子郵件。不幸的是,logcat中給出了這樣的錯誤:

12-25 12:06:17.074: E/AndroidRuntime(8153): FATAL EXCEPTION: main 
12-25 12:06:17.074: E/AndroidRuntime(8153): java.lang.IllegalStateException: Could not execute method of the activity 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at android.view.View$1.onClick(View.java:3063) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at android.view.View.performClick(View.java:3534) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at android.view.View$PerformClick.run(View.java:14263) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at android.os.Handler.handleCallback(Handler.java:605) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at android.os.Handler.dispatchMessage(Handler.java:92) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at android.os.Looper.loop(Looper.java:137) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at android.app.ActivityThread.main(ActivityThread.java:4441) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at java.lang.reflect.Method.invoke(Method.java:511) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at dalvik.system.NativeStart.main(Native Method) 
12-25 12:06:17.074: E/AndroidRuntime(8153): Caused by: java.lang.reflect.InvocationTargetException 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at java.lang.reflect.Method.invoke(Method.java:511) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at android.view.View$1.onClick(View.java:3058) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  ... 11 more 
12-25 12:06:17.074: E/AndroidRuntime(8153): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at android.app.ContextImpl.startActivity(ContextImpl.java:847) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at android.content.ContextWrapper.startActivity(ContextWrapper.java:276) 
12-25 12:06:17.074: E/AndroidRuntime(8153):  at tt.tt.tt.gui.SupportScreen.onClick(SupportScreen.java:38) 

谷歌搜索我發現我需要添加以下行:

emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

這是不可取的,但加入該行仍沒有幫助。那麼,有什麼想法?

回答

0

問題已解決。我不知道爲什麼我使用上下文來運行意圖。

context.startActivity(Intent.createChooser(emailIntent, "Send mail...")); 

應該

startActivity(Intent.createChooser(emailIntent, "Send mail...")); 

,做,這個工程。