【问题标题】:Send My current Location address via email通过电子邮件发送我当前的位置地址
【发布时间】:2013-01-12 17:17:51
【问题描述】:

我有一个问题,我当前的位置地址显示了一个 textView

但问题是点击发送按钮后会报错

但我想通过带有谷歌地图超链接的电子邮件发送我当前的位置

请检查我的问题,请给我任何解决方案

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 // making it full screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);        
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
   ........
 }
 @Override
public void onLocationChanged(Location location) {
    TextView tvLocation = (TextView) findViewById(R.id.tv_location);

    String address = "";
    Geocoder geoCoder = new Geocoder(
            getBaseContext(), Locale.getDefault());

    // Getting latitude
    double latitude = location.getLatitude();

    // Getting longitude
    double longitude = location.getLongitude();

    // Creating an instance of GeoPoint corresponding to latitude and longitude
    GeoPoint point = new GeoPoint((int)(latitude * 1E6), (int)(longitude*1E6));

    try {
        List<Address> addresses = geoCoder.getFromLocation(
            point.getLatitudeE6()  / 1E6, 
            point.getLongitudeE6() / 1E6, 1);

            if (addresses.size() > 0) {
                for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                    address += addresses.get(0).getAddressLine(index) + " ";
            }
            tvLocation.setText("Address :" +  address  );
    }
    catch (IOException e) {                
        e.printStackTrace();
    }
    // Setting latitude and longitude in the TextView tv_location
    tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude +", Add :"+ address );

    // Getting MapController
    MapController mapController = mapView.getController();

    // Locating the Geographical point in the Map
    mapController.animateTo(point);

    // Applying a zoom
    mapController.setZoom(15);

    // Redraw the map
    mapView.invalidate();

    // Getting list of overlays available in the map
    List<Overlay> mapOverlays = mapView.getOverlays();

    // Creating a drawable object to represent the image of mark in the map
    Drawable drawable = this.getResources().getDrawable(R.drawable.cur_position);

    // Creating an instance of ItemizedOverlay to mark the current location in the map
    CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(drawable);

    // Creating an item to represent a mark in the overlay
    OverlayItem currentLocation = new OverlayItem(point, "Current Location", "Latitude : " + latitude + ", Longitude:" + longitude);

    // Adding the mark to the overlay
    currentLocationOverlay.addOverlay(currentLocation);

    // Clear Existing overlays in the map
    mapOverlays.clear();

    // Adding new overlay to map overlay
   mapOverlays.add(currentLocationOverlay); 

}

public void onSendLocationEmail(View button) {

    final TextView txtview = (TextView) findViewById(R.id.tv_location);
    String txt = txtview.getText().toString();

    // Take the fields and format the message contents
    String subject = formatSubject(txt);
    String message = formatMessage(txt);

    sendMessage(subject, message);
    Toast.makeText(getApplicationContext(),"Clicked Send Email Button" + "-", Toast.LENGTH_SHORT).show();
} 

private String formatSubject(String txt) {

    String strSubjectFormat = getResources().getString(R.string.mapsubject_format);

    String strSubject = String.format(strSubjectFormat, txt);

    return strSubject;

}

private String formatMessage(String txt) {
    String strFormatMsg = getResources().getString(R.string.mapbody_format);

 //String strRequiresResponse = getResponseString(bRequiresResponse);

    String strMsg = String.format(strFormatMsg, txt);

    return strMsg;

}

private void sendMessage(String subject, String message) {
    Intent messageIntent = new Intent(android.content.Intent.ACTION_SEND);

    CharSequence location = lastLocation.getLatitudeE6()/ 1E6 + "," + lastLocation.getLongitudeE6()/ 1E6;

    String aEmailList[] = { "srinivasareddy1250@gmail.com" };
    messageIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);

    messageIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);

    messageIntent.setType("plain/text");
    messageIntent.putExtra(android.content.Intent.EXTRA_TEXT, message + "http://maps.google.comk/maps/maps?q=loc:"+location);

}

这是我的错误

不幸的是项目已经停止

01-29 07:34:39.439: E/AndroidRuntime(3693): FATAL EXCEPTION: main
01-29 07:34:39.439: E/AndroidRuntime(3693): java.lang.IllegalStateException: Could not execute method of the activity
01-29 07:34:39.439: E/AndroidRuntime(3693):     at android.view.View$1.onClick(View.java:3597)
01-29 07:34:39.439: E/AndroidRuntime(3693):     at android.view.View.performClick(View.java:4202)
01-29 07:34:39.439: E/AndroidRuntime(3693):     at android.view.View$PerformClick.run(View.java:17340)
01-29 07:34:39.439: E/AndroidRuntime(3693):     at android.os.Handler.handleCallback(Handler.java:725)
01-29 07:34:39.439: E/AndroidRuntime(3693):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-29 07:34:39.439: E/AndroidRuntime(3693):     at android.os.Looper.loop(Looper.java:137)
01-29 07:34:39.439: E/AndroidRuntime(3693):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-29 07:34:39.439: E/AndroidRuntime(3693):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 07:34:39.439: E/AndroidRuntime(3693):     at java.lang.reflect.Method.invoke(Method.java:511)
 01-29 07:34:39.439: E/AndroidRuntime(3693):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-29 07:34:39.439: E/AndroidRuntime(3693):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-29 07:34:39.439: E/AndroidRuntime(3693):     at dalvik.system.NativeStart.main(Native Method)
01-29 07:34:39.439: E/AndroidRuntime(3693): Caused by: java.lang.reflect.InvocationTargetException
01-29 07:34:39.439: E/AndroidRuntime(3693):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 07:34:39.439: E/AndroidRuntime(3693):     at java.lang.reflect.Method.invoke(Method.java:511)
01-29 07:34:39.439: E/AndroidRuntime(3693):     at android.view.View$1.onClick(View.java:3592)
01-29 07:34:39.439: E/AndroidRuntime(3693):     ... 11 more
01-29 07:34:39.439: E/AndroidRuntime(3693): Caused by: java.lang.NullPointerException
 01-29 07:34:39.439: E/AndroidRuntime(3693):    at com.sygnet.locationingooglemap.MainActivity.sendMessage(MainActivity.java:219)
01-29 07:34:39.439: E/AndroidRuntime(3693):     at com.sygnet.locationingooglemap.MainActivity.onSendLocationEmail(MainActivity.java:191)
01-29 07:34:39.439: E/AndroidRuntime(3693):     ... 14 more

【问题讨论】:

  • 你有什么错误?在此处发布堆栈跟踪
  • 用断点调试你的代码,你的 mainactivity 中有一个空指针异常,在第 191 行或第 219 行。可能两者都有。
  • @Tobias Moe Thorstensen 我在那里检查了唯一的问题我不明白请给我解决方案
  • @Satya 我不能告诉你的比LogCat 告诉你的更多;你有一个空指针异常。一步一步调试你的代码,以便找到哪个对象是null
  • 通过查看您的代码,我建议lastLocation 对象在lastLocation.getLatitudeE6() 行为空

标签: android email send currentlocation


【解决方案1】:

这是当前位置地址的完整代码

检查此代码...

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 // making it full screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);        
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    // Getting reference to MapView
    mapView = (MapView) findViewById(R.id.map_view);

    // Setting Zoom Controls on MapView
    mapView.setBuiltInZoomControls(true);



    List<Overlay> mapOverlays = mapView.getOverlays();
    mlo = new MyLocationOverlay(this.getApplicationContext(),mapView);

    if(!mlo.enableMyLocation()){
            Toast.makeText(this, R.string.toast, Toast.LENGTH_LONG).show();
            this.finish();
    }
    mlo.enableCompass();
    mapOverlays.add(mlo);

      ......

  }  
  @Override
public void onLocationChanged(Location location) {
    tvLocation = (TextView) findViewById(R.id.tv_location);

    String address = "";
    Geocoder geoCoder = new Geocoder(
            getBaseContext(), Locale.getDefault());

    // Getting latitude
    latitude = location.getLatitude();

    // Getting longitude
    longitude = location.getLongitude();

    // Creating an instance of GeoPoint corresponding to latitude and longitude
    point = new GeoPoint((int)(latitude * 1E6), (int)(longitude*1E6));

    try {
        List<Address> addresses = geoCoder.getFromLocation(
            point.getLatitudeE6()  / 1E6, 
            point.getLongitudeE6() / 1E6, 1);

            if (addresses.size() > 0) {
                for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                    address += addresses.get(0).getAddressLine(index) + " ";
            }
            tvLocation.setText("Address :" +  address  );
    }
    catch (IOException e) {                
        e.printStackTrace();
    }
    // Setting latitude and longitude in the TextView tv_location
    tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude +", Address :"+ address );

    // Getting MapController
    MapController mapController = mapView.getController();

    // Locating the Geographical point in the Map
    mapController.animateTo(point);

    // Applying a zoom
    mapController.setZoom(15);

    // Redraw the map
    mapView.invalidate();

    // Getting list of overlays available in the map
    List<Overlay> mapOverlays = mapView.getOverlays();

    // Creating a drawable object to represent the image of mark in the map
    Drawable drawable = this.getResources().getDrawable(R.drawable.cur_position);

    // Creating an instance of ItemizedOverlay to mark the current location in the map
    CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(drawable);

    // Creating an item to represent a mark in the overlay
    OverlayItem currentLocation = new OverlayItem(point, "Current Location", "Latitude : " + latitude + ", Longitude:" + longitude);

    // Adding the mark to the overlay
    currentLocationOverlay.addOverlay(currentLocation);

    // Clear Existing overlays in the map
    mapOverlays.clear();

    // Adding new overlay to map overlay
   mapOverlays.add(currentLocationOverlay);         
}

public void onSendLocationEmail(View button) {      

    String txt = tvLocation.getText().toString();

    // Take the fields and format the message contents
    String subject = formatSubject(txt);
    String message = formatMessage(txt);

    sendMessage(subject, message, location);
    Toast.makeText(getApplicationContext(),"Clicked Send Email Button" + "-", Toast.LENGTH_SHORT).show();
} 

private String formatSubject(String txt) {

    String strSubjectFormat = getResources().getString(R.string.mapsubject_format);

    String strSubject = String.format(strSubjectFormat, txt);

    return strSubject;

}

private String formatMessage(String txt) {
    String strFormatMsg = getResources().getString(R.string.mapbody_format);

    String strMsg = String.format(strFormatMsg, txt);

    return strMsg;  
} 

private void sendMessage(String subject, String message,Location location) {

     try {
    Intent messageIntent = new Intent(android.content.Intent.ACTION_SEND);
    messageIntent.setType("plain/text");        

    String aEmailList[] = { "mailId@gmail.com" };
    messageIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);

    messageIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);

    messageIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
            message + "http://maps.google.com/maps?q=loc:" + latitude +","+ longitude);
    startActivity(Intent.createChooser(messageIntent, "Send mail..."));
}
     catch (Exception e) {
            Log.e(LOG_TAG, "sendPictureMessage() failed to start activity.", e);
            Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_LONG).show();
}
}

【讨论】:

    【解决方案2】:
      @Override
    public void onLocationChanged(Location location) {
        tvLocation = (TextView) findViewById(R.id.tv_location);
    
        String address = "";
        Geocoder geoCoder = new Geocoder(
                getBaseContext(), Locale.getDefault());
    
        // Getting latitude
        double latitude = location.getLatitude();
    
        // Getting longitude
        double longitude = location.getLongitude();
    
        // Creating an instance of GeoPoint corresponding to latitude and longitude
        GeoPoint point = new GeoPoint((int)(latitude * 1E6), (int)(longitude*1E6));
    
        try {
            List<Address> addresses = geoCoder.getFromLocation(
                point.getLatitudeE6()  / 1E6, 
                point.getLongitudeE6() / 1E6, 1);
    
                if (addresses.size() > 0) {
                    for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                        address += addresses.get(0).getAddressLine(index) + " ";
                }
                tvLocation.setText("Address :" +  address  );
        }
        catch (IOException e) {                
            e.printStackTrace();
        }
        // Setting latitude and longitude in the TextView tv_location
        tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude +", Add :"+ address );
    
        // Getting MapController
        MapController mapController = mapView.getController();
    
        // Locating the Geographical point in the Map
        mapController.animateTo(point);
    
        // Applying a zoom
        mapController.setZoom(15);
    
        // Redraw the map
        mapView.invalidate();
    
        // Getting list of overlays available in the map
        List<Overlay> mapOverlays = mapView.getOverlays();
    
        // Creating a drawable object to represent the image of mark in the map
        Drawable drawable = this.getResources().getDrawable(R.drawable.cur_position);
    
        // Creating an instance of ItemizedOverlay to mark the current location in the map
        CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(drawable);
    
        // Creating an item to represent a mark in the overlay
        OverlayItem currentLocation = new OverlayItem(point, "Current Location", "Latitude : " + latitude + ", Longitude:" + longitude);
    
        // Adding the mark to the overlay
        currentLocationOverlay.addOverlay(currentLocation);
    
        // Clear Existing overlays in the map
        mapOverlays.clear();
    
        // Adding new overlay to map overlay
       mapOverlays.add(currentLocationOverlay); 
    
    }
    
    public void onSendLocationEmail(View button) {
    
    
        String txt = tvLocation.getText().toString();
    
        // Take the fields and format the message contents
        String subject = formatSubject(txt);
        String message = formatMessage(txt);
    
        sendMessage(subject, message);
        Toast.makeText(getApplicationContext(),"Clicked Send Email Button" + "-", Toast.LENGTH_SHORT).show();
    } 
    
    private String formatSubject(String txt) {
    
        String strSubjectFormat = getResources().getString(R.string.mapsubject_format);
    
        String strSubject = String.format(strSubjectFormat, txt);
    
        return strSubject;
    
    }
    
    private String formatMessage(String txt) {
        String strFormatMsg = getResources().getString(R.string.mapbody_format);
    
    String strMsg = String.format(strFormatMsg, txt);
    
        return strMsg;
    
    } 
    
    private void sendMessage(String subject, String message) {
    
         try {
        Intent messageIntent = new Intent(android.content.Intent.ACTION_SEND);
        messageIntent.setType("plain/text");
    
        String aEmailList[] = { "srinivasareddy1250@gmail.com" };
        messageIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
    
        messageIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    
    
        messageIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
        startActivity(Intent.createChooser(messageIntent, "Send mail..."));
    }
         catch (Exception e) {
                Log.e(LOG_TAG, "sendPictureMessage() failed to start activity.", e);
                Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_LONG).show();
    }
    }
    

    【讨论】:

      【解决方案3】:

      请在点击按钮时尝试使用此代码:

      String phoneNumber = "any contact number here";
       String message = textview1.getText().toString();
       SmsManager smsManager = SmsManager.getDefault();
       smsManager.sendTextMessage(phoneNumber, null, message, null, null);
       Toast.makeText(getApplicationContext(), "Message Sent!", Toast.LENGTH_LONG).show();
      

      【讨论】:

      • 我正在使用这三个权限
      • 请首先告诉我您是否能够在 TextView 中获取位置详细信息?因为我几天前制作了相同的程序,我可以将详细信息发送到另一个联系号码。
      • 我在 TextView 中获得了位置详细信息,但问题只是发送按钮
      • 我已经更新了我的答案,请参见上面...如果有任何疑问,请随时与我联系...如果可能的话,我会帮助您。。
      • 我想发邮件而不是短信
      【解决方案4】:

      试试这个,我得到响应

        double lat = "your lat";
        double lon = "your lng";
      
         List<Address> addresses = new Geocoder(applicationContext,
                          Locale.getDefault()).getFromLocation(lat, lon, 1);
                  if (addresses.size() > 0) {
                      String addressLine = "";
      
                      for (Integer i = 0; i < addresses.get(0)
                              .getMaxAddressLineIndex(); i++) {
                          if (!addressLine.equals("")) {
                              addressLine += ", ";
                          }
      
                          addressLine += addresses.get(0).getAddressLine(i);
                      }
      
                      if (addressLine != "") {
                          foundAddress += addressLine;
                      }
      
                  }
      

      【讨论】:

        猜你喜欢
        • 2014-04-11
        • 2016-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-15
        • 2011-07-01
        • 2016-08-26
        相关资源
        最近更新 更多