我正在尝试检索设备的位置,但遇到了几个问题:
我的日志猫:
07-21 22:23:34.072: E/AndroidRuntime(11634): FATAL EXCEPTION: main
07-21 22:23:34.072: E/AndroidRuntime(11634): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Test.projecttest/com.Test.projecttest.MainActivity}: java.lang.NullPointerException
07-21 22:23:34.072: E/AndroidRuntime(11634): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
07-21 22:23:34.072: E/AndroidRuntime(11634): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
07-21 22:23:34.072: E/AndroidRuntime(11634): at android.app.ActivityThread.access$700(ActivityThread.java:134)
07-21 22:23:34.072: E/AndroidRuntime(11634): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
07-21 22:23:34.072: E/AndroidRuntime(11634): at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 22:23:34.072: E/AndroidRuntime(11634): at android.os.Looper.loop(Looper.java:137)
07-21 22:23:34.072: E/AndroidRuntime(11634): at android.app.ActivityThread.main(ActivityThread.java:4856)
07-21 22:23:34.072: E/AndroidRuntime(11634): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 22:23:34.072: E/AndroidRuntime(11634): at java.lang.reflect.Method.invoke(Method.java:511)
07-21 22:23:34.072: E/AndroidRuntime(11634): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
07-21 22:23:34.072: E/AndroidRuntime(11634): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
07-21 22:23:34.072: E/AndroidRuntime(11634): at dalvik.system.NativeStart.main(Native Method)
07-21 22:23:34.072: E/AndroidRuntime(11634): Caused by: java.lang.NullPointerException
07-21 22:23:34.072: E/AndroidRuntime(11634): at com.Test.projecttest.MainActivity.setUpMap(MainActivity.java:82)
07-21 22:23:34.072: E/AndroidRuntime(11634): at com.Test.projecttest.MainActivity.onCreate(MainActivity.java:71)
07-21 22:23:34.072: E/AndroidRuntime(11634): at android.app.Activity.performCreate(Activity.java:5047)
07-21 22:23:34.072: E/AndroidRuntime(11634): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-21 22:23:34.072: E/AndroidRuntime(11634): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
07-21 22:23:34.072: E/AndroidRuntime(11634): ... 11 more
我的 MainActivity.java 代码:
public class MainActivity extends FragmentActivity implements OnClickListener, LocationListener {
private GoogleMap map;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SupportMapFragment fmap = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
map = fmap.getMap();
setUpMap();
}
private void setUpMap() {
map.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
map.setMapType(map.MAP_TYPE_HYBRID);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map.animateCamera(CameraUpdateFactory.zoomTo(20));
map.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("I'M HERE"));
}
}
我也尝试使用 google.developer.com 上的代码,但是它太混乱了,无法真正理解其中的大多数,并且在我尝试运行它们时出现错误。我应该怎么做才能解决这个问题?评论表示赞赏。:)