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 に答える