【问题标题】:variables are not submitted to the email变量未提交到电子邮件
【发布时间】:2018-01-14 08:58:10
【问题描述】:

所以我按照question的回答:

我尝试将它集成到我的代码中,但它不起作用。

基本上我想获取当前坐标,然后将它们提交到电子邮件地址。

这是来自答案,应该获取坐标并将它们写入变量'lat'和'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;
    }
}

然后我尝试在单击按钮时发送电子邮件。这将打开电子邮件应用程序。但应用程序应该自己编写所有文本。 (我也可以在后台简单地发送它吗?)在正文中应该有一个小文本,然后是我的坐标,我在变量“lat”和“lon”中定义。可悲的是它不承认我的变量。我需要改变什么?

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();
                }

            }
        });

编辑

这是堆栈跟踪中的错误:

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)

【问题讨论】:

    标签: android email variables


    【解决方案1】:

    您已经在代码中调用了getLocation() 方法,但您没有在任何地方使用返回值。请执行以下操作 -

    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();
                    }
    
                }
            });
    

    在这里,我将返回值保存在 latlng 变量中,然后使用它发送到文本。您还可以修改 put extra 行来执行此操作 -

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

    这将为您提供格式中的经纬度 -

    纬度/经度:(xx.xxxxxx,yy.yyyyy)

    【讨论】:

    • 在 i.putExtra 行中找不到“getLatitude”
    • @MrHarrison 更新了我的答案。由于它们是公共变量,因此可以直接访问它们。刚刚交叉检查了类定义。
    • 谢谢。有没有不用添加太多代码在后台发送邮件的解决方案?
    • 不,那里没有捷径,您可能需要使用 gmail 设置 oAuth,然后使用它发送它,但它的代码不少。因此,如果您想进行简短设置,则需要人工干预。检查此自动解决方案 - stackoverflow.com/questions/6517079/…
    • @MrHarrison 另外,如果可行,请接受我的回答:)
    猜你喜欢
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多