大家好,我是一名Android程序员的新手。我已经构建了包含GPS状态的发送电子邮件意图方法。我有一个按钮,如果用户单击此按钮,我使用if函数检查GPS的状态并发送电子邮件。但使用意图发送电子邮件不起作用,并且在LogCat中显示以下错误:
07-16 16:23:30.161: E/AndroidRuntime(28567): FATAL EXCEPTION: main
07-16 16:23:30.161: E/AndroidRuntime(28567): java.lang.IllegalStateException: Could not execute method of the activity
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.view.View$1.onClick(View.java:3735)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.view.View.performClick(View.java:4354)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.view.View$PerformClick.run(View.java:17961)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.os.Handler.handleCallback(Handler.java:725)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.os.Handler.dispatchMessage(Handler.java:92)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.os.Looper.loop(Looper.java:137)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.app.ActivityThread.main(ActivityThread.java:5328)
07-16 16:23:30.161: E/AndroidRuntime(28567): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 16:23:30.161: E/AndroidRuntime(28567): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 16:23:30.161: E/AndroidRuntime(28567): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-16 16:23:30.161: E/AndroidRuntime(28567): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-16 16:23:30.161: E/AndroidRuntime(28567): at dalvik.system.NativeStart.main(Native Method)
07-16 16:23:30.161: E/AndroidRuntime(28567): Caused by: java.lang.reflect.InvocationTargetException
07-16 16:23:30.161: E/AndroidRuntime(28567): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 16:23:30.161: E/AndroidRuntime(28567): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.view.View$1.onClick(View.java:3730)
07-16 16:23:30.161: E/AndroidRuntime(28567): ... 11 more
07-16 16:23:30.161: E/AndroidRuntime(28567): Caused by: java.lang.IllegalArgumentException: Plain text must be supplied if HTML text is supplied
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.content.ClipData$Item.<init>(ClipData.java:252)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.content.Intent.migrateExtraStreamToClipData(Intent.java:7253)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.content.Intent.migrateExtraStreamToClipData(Intent.java:7234)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1428)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.app.Activity.startActivityForResult(Activity.java:3430)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.app.Activity.startActivityForResult(Activity.java:3391)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.app.Activity.startActivity(Activity.java:3626)
07-16 16:23:30.161: E/AndroidRuntime(28567): at android.app.Activity.startActivity(Activity.java:3594)
07-16 16:23:30.161: E/AndroidRuntime(28567): at com.mpa.emvi.HomeActivity.sendEmail(HomeActivity.java:100)
07-16 16:23:30.161: E/AndroidRuntime(28567): ... 14 more
好的,在我的HomActivityClass中
package com.mpa.emvi;
import com.mpa.emvi.R;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
public class HomeActivity extends FragmentActivity
implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private LocationManager mLocationManager;
boolean statusOfGPS;
private GoogleMap mMap;
private LocationClient mLocationClient;
private String DescribText = new String();
private static final String TAG = "MyGPS_status";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
setUpLocationClientIfNeeded();
mLocationClient.connect();
mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE );
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setMyLocationEnabled(true);
}
}
}
private void setUpLocationClientIfNeeded() {
if (mLocationClient == null) {
mLocationClient = new LocationClient(
getApplicationContext(),
this, // ConnectionCallbacks
this); // OnConnectionFailedListener
}
}
public void sendEmail(View view){
statusOfGPS = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Intent i = new Intent(Intent.ACTION_SEND);
if (mLocationClient != null && mLocationClient.isConnected() && statusOfGPS == true) {
DescribText = "This is My Emergency Location on GPS " +
"<a href='https://maps.google.com/maps?q='"+mLocationClient.getLastLocation().getLatitude()+"','"+mLocationClient.getLastLocation().getLongitude()+"'&ll='"+mLocationClient.getLastLocation().getLatitude()+"','"+mLocationClient.getLastLocation().getLongitude()+"'&z=17'>click here</a>";
i.setType("text/html");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"suphawut@mpatmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "Emergency Location");
i.putExtra(Intent.EXTRA_HTML_TEXT , DescribText);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_LONG).show();
}
Log.d(TAG, "gps =" + statusOfGPS);
}else{
DescribText = "This is My Emergency Location on INTERNET" +
" <a href='https://maps.google.com/maps?q='"+mLocationClient.getLastLocation().getLatitude()+"','"+mLocationClient.getLastLocation().getLongitude()+"'&ll='"+mLocationClient.getLastLocation().getLatitude()+"','"+mLocationClient.getLastLocation().getLongitude()+"'&z=17'>click here</a>";
i.setType("text/html");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"suphawut@mpatmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "Emergency Location");
i.putExtra(Intent.EXTRA_HTML_TEXT , DescribText);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_LONG).show();
}
Log.d(TAG, "gps =" + statusOfGPS);
}
}
@Override
protected void onPause() {
super.onPause();
}
/**
* Callback called when disconnected from GCore. Implementation of {@link ConnectionCallbacks}.
*/
@Override
public void onDisconnected() {
// Do nothing
}
/**
* Implementation of {@link OnConnectionFailedListener}.
*/
@Override
public void onConnectionFailed(ConnectionResult result) {
// Do nothing
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
}
public void onProviderEnable(String provider){
}
}
发送电子邮件的意图在sendEmail方法中,为什么这个方法可以工作?
注:我的英语水平较差,感谢任何答案。