我正在尝试从我的电子邮件类发送电子邮件,但是当程序进入startActivity时它会崩溃,我认为它可能与清单有关。以下是我的主要活动
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class InvoiceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void SendMessage(View v)
{
// get email parameters
SMTPmail mail = new SMTPmail();
mail.SendSMTP("body of email","subject of email","recipient@example.com");
}
}
这是SMTPmail
import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;
public class SMTPmail extends Activity {
public void SendSMTP(String message, String subject, String recipted)
{
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{recipted});
i.putExtra(Intent.EXTRA_CC, "");
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT , message);
try {
startActivity(Intent.createChooser(i, "Send mail..."));//crashes here in debug
finish();
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(SMTPmail.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
}
这是清单
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".InvoiceActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SMTPmail"
android:noHistory="true">
</activity>
</application>
下面的logcat
01-07 00:01:25.199: D/dalvikvm(14198): GC_EXTERNAL_ALLOC freed 43K, 51% free 2687K/5379K, external 0K/0K, paused 54ms
01-07 00:01:30.389: D/AndroidRuntime(14198): Shutting down VM
01-07 00:01:30.389: W/dalvikvm(14198): threadid=1: thread exiting with uncaught exception (group=0x40018560)
01-07 00:01:30.399: E/AndroidRuntime(14198): FATAL EXCEPTION: main
01-07 00:01:30.399: E/AndroidRuntime(14198): java.lang.IllegalStateException: Could not execute method of the activity
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.view.View$1.onClick(View.java:2165)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.view.View.performClick(View.java:2506)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.view.View$PerformClick.run(View.java:9112)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.os.Handler.handleCallback(Handler.java:587)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.os.Looper.loop(Looper.java:130)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.app.ActivityThread.main(ActivityThread.java:3835)
01-07 00:01:30.399: E/AndroidRuntime(14198): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 00:01:30.399: E/AndroidRuntime(14198): at java.lang.reflect.Method.invoke(Method.java:507)
01-07 00:01:30.399: E/AndroidRuntime(14198): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
01-07 00:01:30.399: E/AndroidRuntime(14198): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
01-07 00:01:30.399: E/AndroidRuntime(14198): at dalvik.system.NativeStart.main(Native Method)
01-07 00:01:30.399: E/AndroidRuntime(14198): Caused by: java.lang.reflect.InvocationTargetException
01-07 00:01:30.399: E/AndroidRuntime(14198): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 00:01:30.399: E/AndroidRuntime(14198): at java.lang.reflect.Method.invoke(Method.java:507)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.view.View$1.onClick(View.java:2160)
01-07 00:01:30.399: E/AndroidRuntime(14198): ... 11 more
01-07 00:01:30.399: E/AndroidRuntime(14198): Caused by: java.lang.NullPointerException
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.app.Activity.startActivityForResult(Activity.java:2827)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.app.Activity.startActivity(Activity.java:2933)
01-07 00:01:30.399: E/AndroidRuntime(14198): at com.android.EUROPE.Invoice.SMTPmail.SendSMTP(SMTPmail.java:17)
01-07 00:01:30.399: E/AndroidRuntime(14198): at com.android.EUROPE.Invoice.InvoiceActivity.SendMessage(InvoiceActivity.java:19)
01-07 00:01:30.399: E/AndroidRuntime(14198): ... 14 more
答案 0 :(得分:2)
正如詹姆斯已经说过的那样,SMTPmail
没有理由延长Activity
。一个简单的实现可能看起来像这样。
SMTPmail
:
public class SMTPmail {
public static void sendSMTP(Context context, String message, String subject, String recipted)
{
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{recipted});
i.putExtra(Intent.EXTRA_CC, "");
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT , message);
try {
context.startActivity(Intent.createChooser(i, "Send mail..."));//crashes here in debug
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
}
在InvoiceActivity
:
public void SendMessage(View v)
{
SMTPmail.sendSMTP(this, "body of email","subject of email","recipient@example.com");
}
答案 1 :(得分:1)
有SMTPmail
延伸Activity
的原因吗?这看起来多余。只需更改STMPmail,以便它不会扩展任何类,这应该工作。您必须将Context
传递给该方法才能获得startActivity()
。