接收位置作为输入并在android中计算距离时出错

时间:2017-01-22 01:46:12

标签: android location distance

我将纬度和经度作为用户输入并计算两点之间的距离。我收到以下错误。

这是代码:

package com.example.garima.cal;

import android.location.Location;
import android.nfc.Tag;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.vision.barcode.Barcode;

public class MainActivity extends FragmentActivity  {

    private GoogleMap mMap;
    private GoogleMap googleMap;
    EditText etLng;
    EditText etLat;
    EditText etLng2;
    EditText etLat2;
    private static LatLng point,point2;
    private String TAG ="Main Activity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.

        // Reference to the EditText et_lat of the layout activity_main.xml
        etLat = (EditText) findViewById(R.id.et_lat);

        // Reference to the EditText et_lng of the layout activity_main.xml
        etLng = (EditText) findViewById(R.id.et_lng);

        etLat2 = (EditText) findViewById(R.id.et_lat2);
        etLng2 = (EditText) findViewById(R.id.et_lng2);

        etLng.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {

                    // Getting the entered latitude
                    String lat = etLat.getText().toString();

                    // Getting the entered longitude
                    String lng = etLng.getText().toString();

                    // Getting the entered latitude

                        // LatLng object to store user input coordinates
                         point = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));

                }
                return false;
            }
        });
        etLng2.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {

                    // Getting the entered latitude
                    String lat2 = etLat2.getText().toString();

                    // Getting the entered longitude
                    String lng2 = etLng2.getText().toString();

                    // Getting the entered latitude

                    // LatLng object to store user input coordinates

                     point2=new LatLng(Double.parseDouble(lat2), Double.parseDouble(lng2));
                    calculateDist(point,point2);
                }
                return false;
            }
        });

        //calculate distance

        //Log.i(TAG,"The calculated distance is "+distance);

    }
    private void calculateDist(LatLng point,LatLng point2){
        float [] dist = new float[1];
        Location.distanceBetween(point.longitude,point.latitude,point2.longitude,point2.latitude,dist);

        String distances = String.valueOf(dist[0]);
        TextView distanceView = (TextView) findViewById(R.id.txt_distance);
        System.out.println(distances);
        distanceView.setText(distances+" m");

    }
}

这是错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: com.example.garima.taxfix_cal, PID: 2952
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.garima.taxfix_cal/com.example.garima.taxfix_cal.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'double com.google.android.gms.maps.model.LatLng.longitude' on a null object reference
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                 at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:135)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                              Caused by: java.lang.NullPointerException: Attempt to read from field 'double com.google.android.gms.maps.model.LatLng.longitude' on a null object reference
                                                 at com.example.garima.taxfix_cal.MainActivity.calculateDist(MainActivity.java:118)
                                                 at com.example.garima.taxfix_cal.MainActivity.onCreate(MainActivity.java:111)
                                                 at android.app.Activity.performCreate(Activity.java:5937)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                 at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:135) 
                                                 at android.app.ActivityThread.main(ActivityThread

0 个答案:

没有答案