-2

我正在尝试使用 LocationManager 查找手机当前位置的经度和纬度。但是当我调用 getLongitude 时,我得到一个空指针异常。可能有一个简单的解决方案。有人可以帮我吗?

import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    private LocationManager locationManager;
    private String provider;

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

        getCord();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void getCord() {
        final TextView text = (TextView) findViewById(R.id.textView);
        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
        boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if (!enabled) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }

        // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // defaultazz
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        //int lng = (int) (location.getLongitude());

        text.setText("" +location.getLatitude());
    }

}

猫:

11-21 14:53:35.830: E/AndroidRuntime(722): FATAL EXCEPTION: main
11-21 14:53:35.830: E/AndroidRuntime(722): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.envio/com.example.envio.MainActivity}: java.lang.NullPointerException
11-21 14:53:35.830: E/AndroidRuntime(722):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-21 14:53:35.830: E/AndroidRuntime(722):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-21 14:53:35.830: E/AndroidRuntime(722):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-21 14:53:35.830: E/AndroidRuntime(722):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-21 14:53:35.830: E/AndroidRuntime(722):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 14:53:35.830: E/AndroidRuntime(722):  at android.os.Looper.loop(Looper.java:137)
11-21 14:53:35.830: E/AndroidRuntime(722):  at android.app.ActivityThread.main(ActivityThread.java:5039)
11-21 14:53:35.830: E/AndroidRuntime(722):  at java.lang.reflect.Method.invokeNative(Native Method)
11-21 14:53:35.830: E/AndroidRuntime(722):  at java.lang.reflect.Method.invoke(Method.java:511)
11-21 14:53:35.830: E/AndroidRuntime(722):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-21 14:53:35.830: E/AndroidRuntime(722):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-21 14:53:35.830: E/AndroidRuntime(722):  at dalvik.system.NativeStart.main(Native Method)
11-21 14:53:35.830: E/AndroidRuntime(722): Caused by: java.lang.NullPointerException
11-21 14:53:35.830: E/AndroidRuntime(722):  at com.example.envio.MainActivity.getCord(MainActivity.java:54)
11-21 14:53:35.830: E/AndroidRuntime(722):  at com.example.envio.MainActivity.onCreate(MainActivity.java:24)
11-21 14:53:35.830: E/AndroidRuntime(722):  at android.app.Activity.performCreate(Activity.java:5104)
11-21 14:53:35.830: E/AndroidRuntime(722):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-21 14:53:35.830: E/AndroidRuntime(722):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-21 14:53:35.830: E/AndroidRuntime(722):  ... 11 more
4

4 回答 4

0

你读过错误信息吗?

11-21 14:53:35.830: E/AndroidRuntime(722): Caused by: java.lang.NullPointerException
11-21 14:53:35.830: E/AndroidRuntime(722):  at com.example.envio.MainActivity.getCord(MainActivity.java:54)

似乎您有一个在第 54 行返回 Null 的方法,但您没有检查。

于 2012-11-21T15:35:50.607 回答
0

修改您的getCord方法,如下所示:

public void getCord() {
    final TextView text = (TextView) findViewById(R.id.textView);
    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);

    if(service != null){
      if (!service.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
      }
      else{
        // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // defaultazz
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        //int lng = (int) (location.getLongitude());

        text.setText("" +location.getLatitude());
      }
    }
}
于 2012-11-21T15:38:07.400 回答
0

您的变量位置为空,您必须测试: if(location!=null) TODO ..

于 2012-11-21T15:38:47.493 回答
0

尝试

locationManager = (LocationManager) getSystemService(context.LOCATION_SERVICE);

Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);

 if (locationManager != null) {
        if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            location = locationManager.getLastKnownLocation(provider);
        }
    }

    if (location != null) {
        onLocationChanged(location);
    } else {
        text.setText("Location not available");

    }
}

然后覆盖 onLocationChange 方法:

@覆盖

public void onLocationChanged(Location location) {
    double lat = (double) (location.getLatitude());
    double lng = (double) (location.getLongitude());

    text.setText(String.valueOf(lat));


}

这对我有用

于 2017-03-03T12:33:25.320 回答