我的位置抛出空指针异常?

时间:2012-11-26 02:50:25

标签: android eclipse google-maps geolocation gps

昨天!我写代码找到位置!这是工作。 但是今天,当我在diffirent函数中编写更多代码时,它不起作用。并抛出一个例外 你能帮助我吗?? HIX!我是新手。 这是日志:

    11-26 09:44:52.109: E/AndroidRuntime(9259): FATAL EXCEPTION: main
11-26 09:44:52.109: E/AndroidRuntime(9259): java.lang.NullPointerException
11-26 09:44:52.109: E/AndroidRuntime(9259):     at com.example.zzzzzz.MainActivity.onOptionsItemSelected(MainActivity.java:351)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at android.app.Activity.onMenuItemSelected(Activity.java:2205)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:777)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at android.view.View$PerformClick.run(View.java:9112)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at android.os.Handler.handleCallback(Handler.java:587)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at android.os.Looper.loop(Looper.java:130)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at android.app.ActivityThread.main(ActivityThread.java:3835)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at java.lang.reflect.Method.invokeNative(Native Method)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at java.lang.reflect.Method.invoke(Method.java:507)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-26 09:44:52.109: E/AndroidRuntime(9259):     at dalvik.system.NativeStart.main(Native Method)
11-26 09:44:53.939: I/Process(9259): Sending signal. PID: 9259 SIG: 9

这是我查找位置的功能:

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.about:
        //about
        Intent about = new Intent("com.example.zzzzzz.About");
        startActivity(about);
        break;
    case R.id.setting:
        //setting
        Intent setting = new Intent("com.example.zzzzzz.Setting");
        startActivityForResult(setting, 2);
        break;
    case R.id.direction:
        //direction
        Intent direction = new Intent("com.example.zzzzzz.Direction");
        startActivityForResult(direction, 1);
        break;
    case R.id.MyLocation:
        //mylocation
        //Intent mylocation = new Intent("com.example.zzzzzz.MyLocation");
        //startActivity(mylocation);
        initLocationManagerV2();
        LocationManager locationManager;
        locationManager = (LocationManager)getSystemService (Context.LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation (LocationManager.GPS_PROVIDER);
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        GeoPoint find = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
        mv.getController().animateTo(find);
        mv.invalidate();
        break;
    }

    return false;
}

这是函数initLocationMaganeV2:

private void initLocationManagerV2() {
    locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locListener = new LocationListener() {

        public void onLocationChanged(Location newLocation) {
            createAndShowCustomOverlay(newLocation);
        }

        public void onProviderDisabled(String arg0) {
        }

        public void onProviderEnabled(String arg0) {
        }

        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        }
    };
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            locListener);
}

protected void createAndShowCustomOverlay(Location newLocation) {
    List overlays = mv.getOverlays();

    // first remove old overlay
    if (overlays.size() > 0) {
        for (Iterator iterator = overlays.iterator(); iterator
                .hasNext();) {
            iterator.next();
            iterator.remove();
        }
    }

    // transform the location to a geopoint
    GeoPoint geopoint = new GeoPoint(
            (int) (newLocation.getLatitude() * 1E6), (int) (newLocation
                    .getLongitude() * 1E6));

    // Create new Overlay
    CustomOverlay overlay = new CustomOverlay(geopoint);

    mv.getOverlays().add(overlay);

    // move to location
    //mv.getController().animateTo(geopoint);

    // redraw map
    mv.postInvalidate();
}

2 个答案:

答案 0 :(得分:0)

最有可能

Location location = locationManager.getLastKnownLocation (LocationManager.GPS_PROVIDER);

返回 null ,您无需检查即可使用它。

答案 1 :(得分:0)

有关getLastKnownLocation(提供程序)的Google文档声明“如果提供程序当前已禁用,则返回null”。每次使用上面的代码测试您的例程时,请确保您的GPS已启用

locationManager.getLastKnownLocation (LocationManager.GPS_PROVIDER);