2017-08-07 79 views
0

我得到以下代码。我正在尝试将我的坐标提交给电子邮件。应用程序关闭,它应提交数据

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

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[]{"[email protected]"}); 
        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) 

所以,问题是:究竟是什么问题呢?不知何故坐标也不会通过日志显示。

+0

您需要执行某些步骤获得的位置,以便于我,它看起来像你的代码是不完整的。我在下面的答案中提到了一些示例教程中的步骤。看一看。 –

回答

0

在对此对象执行任何操作之前,您应该先检查latlng

例如

LatLng latlng = getLocation(); 


Intent i = new Intent(Intent.ACTION_SENDTO); 
//i.setType("message/rfc822"); 
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
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(); 
} 
0

如果该位置存储在设备中,此代码也只会为您提供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/

+0

我要试一试。稍后再评论 – MrHarrison

+0

如果我正在尝试在新应用程序中执行此操作,则第二个教程不起作用 – MrHarrison

+0

也添加了第三个教程。所有这些都会给你如何去做的基本想法。如果它不起作用,我们将不得不调试问题,因为所有这些都已经过测试。 :) –