我正在尝试使用 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