0

昨天!我写了代码来查找位置!这是工作。但是今天,当我在不同的功能上编写更多代码时,它就不起作用了。并抛出异常你能帮帮我吗?嗨!我是新手。这是日志:

    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();
}
4

2 回答 2

0

getLastKnownLocation(provider) 上的 Google 文档指出“如果提供程序当前被禁用,则返回 null。” 确保在您使用上面的代码测试您的例程时启用您的 GPS,因为您已经

locationManager.getLastKnownLocation (LocationManager.GPS_PROVIDER);
于 2012-11-26T03:16:20.993 回答
0

最可能

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

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

于 2012-11-26T03:03:57.887 回答