2012-12-25 45 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...")); 

,做,这个工程。