应用程序应提交数据时关闭 [英] App closes while it should submit data

查看:85
本文介绍了应用程序应提交数据时关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I got the following code. I'm trying to submit my coordinates to an email.

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

But every time I press the button, the app closes.

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

                }
            });
        }
    });

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

If I press the button, the following error from the stack trace appears:

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)

所以问题是:到底是什么问题?坐标也无法通过日志显示出来。

So the question is: what is exactly the problem? Somehow the coordinates are not shown through the Log too.

推荐答案

如果位置存储在设备中,则此代码也只为您提供LastKnownLocation。它并不能始终为您提供最新的位置。如果需要获取位置,则需要执行某些步骤。

This code only gives you the LastKnownLocation that too if the location is stored in the device. It does not give you the most current location necessarily all the time. If you need to get the Location, you need to perform certain steps.


  1. 请求访问位置的权限-ACCESS_FINE_LOCATION

  2. 创建一个实现 android.location.LocationListener
  3. 的Location Manager类
  4. 使用<$ c $来启动位置服务c>(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

  5. 像上面一样检查最后一个已知位置。

  6. 如果使用 requestLocationUpdates()侦听器方法无法请求当前位置。

  7. 实施 onLocationChanged()方法并获取该方法中的当前位置。

  8. 在收到位置后执行操作。

  9. 工作结束后,请禁用监听器。

  1. Request Permission to Access Location - ACCESS_FINE_LOCATION
  2. Create a Location Manager class that implements android.location.LocationListener
  3. Initiate the Location Service using (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  4. Check for Last known Location as you have done above.
  5. If its not available request for the current location using requestLocationUpdates() listener method.
  6. Implement onLocationChanged() method and get the current location in that method.
  7. Perform your operation once the location is received.
  8. Disable the listener once the work is done.

您可以按照以下提到的链接查看上述步骤的工作方式。

You can follow the below mentioned links to see how the above steps work out.

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/

这篇关于应用程序应提交数据时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆