0

So I followed an answer to this question:

I tried to integrate it into my code, but it doesn't work.

Basically I want to get the current coordinates and then submit them to an email adress.

This is from an answer and should get the coordinates and write them into the variables 'lat' and 'lon'

public LatLng getLocation()
{
    // Get the location manager
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    Double lat;
    double lon;
    try {
        lat = location.getLatitude();
        lon = location.getLongitude();
        return new LatLng(lat, lon);
    }
    catch (NullPointerException e){
        e.printStackTrace();
        return null;
    }
}

Then I try to send an email, when the button is clicked. This will open the email application. But the app should write all the text herself. (Can I simple send it in the background too?) In the body there should be a little text and then my coordinates, which I defined in the variables 'lat' and 'lon'. Sadly it doesn't recognise my variables. What do I have to change?

final Button button = (Button) findViewById(R.id.addbutton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                getLocation();
                Intent i = new Intent(Intent.ACTION_SENDTO);
                //i.setType("message/rfc822");
                i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"mailadress@example.com"});
                i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
                i.putExtra(Intent.EXTRA_TEXT   , lat + lon);
                try {
                    startActivity(Intent.createChooser(i, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(MapsActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                }

            }
        });

Edit

This is the error in the stacktrace:

08-07 09:59:58.339 31371-31371/com.example.testingmapingmarker23 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.example.testingmapingmarker23, PID: 31371
                                                                                   Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
                                                                                   java.lang.NullPointerException: Attempt to read from field 'double com.google.android.gms.maps.model.LatLng.latitude' on a null object reference
                                                                                       at com.example.testingmapingmarker23.MapsActivity$1$1.onClick(MapsActivity.java:91)
                                                                                       at android.view.View.performClick(View.java:5204)
                                                                                       at android.view.View$PerformClick.run(View.java:21158)
                                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
MrHarrison
  • 15
  • 1
  • 9

1 Answers1

0

You have called your getLocation() method in the code but you have not used the return value anywhere. Please do as follows -

final Button button = (Button) findViewById(R.id.addbutton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                LatLng latlng = getLocation();
                Intent i = new Intent(Intent.ACTION_SENDTO);
                //i.setType("message/rfc822");
                i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"mailadress@example.com"});
                i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
                i.putExtra(Intent.EXTRA_TEXT   , latlng.latitude+" " + latlng.longitude);
                try {
                    startActivity(Intent.createChooser(i, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(MapsActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                }

            }
        });

Here i am saving the return value in latlng variable and then using it to send to text. You can also modify the put extra line to do this -

i.putExtra(Intent.EXTRA_TEXT   , latlng.toString());

This will give you the lat long in the format -

lat/lng: (xx.xxxxxx,yy.yyyyy)

Kapil G
  • 4,081
  • 2
  • 20
  • 32
  • It can not find 'getLatitude' in the i.putExtra line – MrHarrison Aug 07 '17 at 06:33
  • @MrHarrison updated my answer. Since they are public variables they can be accessed directly. Just crosschecked the class definition. – Kapil G Aug 07 '17 at 06:37
  • thank you. Do you also have a solution for sending the mail in the background without adding too much code? – MrHarrison Aug 07 '17 at 06:39
  • Nope no shortcut there, you might have tos etup oAuth using gmail and then send it using that but its not little code. So human intervention is required if you want to do short setup. Check this for automatic solution - https://stackoverflow.com/questions/6517079/send-email-in-service-without-prompting-user – Kapil G Aug 07 '17 at 06:41
  • @MrHarrison Also if it works please do accept my answer :) – Kapil G Aug 07 '17 at 06:42
  • It doesn't work. If I press the button both variantes the app closes immidiately. – MrHarrison Aug 07 '17 at 07:43
  • @what does the crash say? – Kapil G Aug 07 '17 at 07:51
  • @MrHarrison are you sure getLocation method returns location. Have you tried printing it there and seeing if it works. – Kapil G Aug 07 '17 at 08:06
  • No. How to do it? – MrHarrison Aug 07 '17 at 08:13
  • before returning in return new LatLng(lat, lon); just do Log.i("logging","lat "+lat+" long "+lon); – Kapil G Aug 07 '17 at 08:17
  • It doesn't show anything. It's kind of interesting that there is no GPS Singal sign in the top bar. If I have Google Maps opened with GPS the signal appears. – MrHarrison Aug 07 '17 at 08:20
  • That means your logic for getting the location is incorrect and has to be modified :) Also have you taken the permission to Access the location, both runtime and in manifest – Kapil G Aug 07 '17 at 08:22
  • Can you post a new query for that with all relevant code and accept this answer as this is different from that :) – Kapil G Aug 07 '17 at 08:22
  • Maybe you can answer to the other question too: https://stackoverflow.com/questions/45542559/app-closes-while-it-should-submit-data – MrHarrison Aug 07 '17 at 08:33