2017-08-07 1 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) 

そこで質問です:問題を正確には何ですか?どういうわけか座標はLogを介しても表示されません。

+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

新しいアプリケーションでこれをやろうとしていると第2のチュートリアルがうまくいきません – MrHarrison

+0

3番目のチュートリアルも追加されました。これらのすべてがあなたにそれを行う方法の基本的なアイデアを与えるでしょう。動作しない場合は、これらのすべてがテストされているため、どこに問題があるのか​​をデバッグする必要があります。 :) –

関連する問題