错误:活动外的呼叫活动



i确实有一个屏幕(活动)的应用程序。从其中一个(即支持屏幕)中,我必须能够运行电子邮件,共享其他活动。因此,我以这种方式添加了发送电子邮件选项:

 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[]{"elitedrumbeat@gmail.com"});
            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);

这不是可取的,但是添加该行仍然没有帮助。那么,有什么想法吗?

问题已经解决。我不知道为什么我将上下文用于跑步意图。

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

应该是

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

完成,这有效。