错误:从活动外部调用活动

时间:2012-12-25 07:14:11

标签: android android-intent

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

 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);

哪个不是优选的,但添加该行仍然没有帮助。那么,有什么想法吗?

1 个答案:

答案 0 :(得分:0)

问题已经解决。我不知道为什么我使用上下文来运行意图。

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

应该是

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

完成后,这有效。