【问题标题】:App closes while it should submit data应用程序在应提交数据时关闭
【发布时间】:2017-08-07 08:32:43
【问题描述】:

我得到了以下代码。我正在尝试将我的坐标提交到电子邮件中。

但每次我按下按钮时,应用程序都会关闭。

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();
            Log.i("logging","lat "+lat+" long "+lon);
            return new LatLng(lat, lon);
        }
        catch (NullPointerException e){
            e.printStackTrace();
            return null;
        }
    }

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    final Button button = (Button) findViewById(R.id.addbutton);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            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) {
                        makeText(MapsActivity.this, "There are no email clients installed.", LENGTH_SHORT).show();
                    }

                }
            });
        }
    });

如果我按下按钮,堆栈跟踪中会出现以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.testingmapingmarker23, PID: 6630
                                                                                 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:96)
                                                                                     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)

所以问题是:到底是什么问题?不知何故,坐标也没有通过日志显示。

【问题讨论】:

  • 您需要执行某些步骤来获取位置,所以在我看来您的代码不完整。我在下面的答案中提到了一些示例教程中的步骤。看看吧。

标签: email coordinates


【解决方案1】:

在您想对该对象执行任何操作之前,您应该检查 latlng 是否为空。

例如

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");
if(latlng != null) {
    i.putExtra(Intent.EXTRA_TEXT   , latlng.latitude+" " + latlng.longitude);
} else {
    i.putExtra(Intent.EXTRA_TEXT   , "location not available");
}

try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    makeText(MapsActivity.this, "There are no email clients installed.", LENGTH_SHORT).show();
}

【讨论】:

    【解决方案2】:

    如果位置存储在设备中,此代码只会为您提供 LastKnownLocation。它不一定总是为您提供最新的位置。如果您需要获取位置,则需要执行某些步骤。

    1. 请求访问位置的权限 - ACCESS_FINE_LOCATION
    2. 创建实现android.location.LocationListener 的位置管理器类
    3. 使用(LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 启动定位服务
    4. 按照上面的操作检查最后一个已知位置。
    5. 如果当前位置的请求不可用,则使用requestLocationUpdates() 侦听器方法。
    6. 实现onLocationChanged() 方法并在该方法中获取当前位置。
    7. 收到位置后执行您的操作。
    8. 工作完成后禁用监听器。

    您可以按照下面提到的链接查看上述步骤的效果。

    https://www.codota.com/android/scenarios/52fcbdd6da0a6fdfa462fe3b/finding-current-location?tag=dragonfly&fullSource=1

    https://www.tutorialspoint.com/android/android_location_based_services.htm

    http://javapapers.com/android/get-current-location-in-android/

    【讨论】:

    • 我要试试这个。稍后发表评论
    • 如果我想在新应用中这样做,第二个教程不起作用
    • 还添加了第三个。所有这些都将为您提供如何做到这一点的基本概念。如果它不起作用,我们将不得不调试问题出在哪里,因为所有这些都已经过测试。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    相关资源
    最近更新 更多