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[]{"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);

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

4

1 回答 1

0

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

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

应该

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

完成,这行得通。

于 2012-12-25T08:22:50.790 回答