-1

log-cat 中的错误是,

 10-27 06:07:31.428: E/AndroidRuntime(17826): FATAL EXCEPTION: main
    10-27 06:07:31.428: E/AndroidRuntime(17826): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
    10-27 06:07:31.428: E/AndroidRuntime(17826):    at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
    10-27 06:07:31.428: E/AndroidRuntime(17826):    at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
    10-27 06:07:31.428: E/AndroidRuntime(17826):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:279)
    10-27 06:07:31.428: E/AndroidRuntime(17826):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
    10-27 06:07:31.428: E/AndroidRuntime(17826):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    10-27 06:07:31.428: E/AndroidRuntime(17826):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    10-27 06:07:31.428: E/AndroidRuntime(17826):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    10-27 06:07:31.428: E/AndroidRuntime(17826):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    10-27 06:07:31.428: E/AndroidRuntime(17826):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
    10-27 06:07:31.428: E/AndroidRuntime(17826):    

package pt.up.fe.aroundme.android;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import pt.up.fe.aroundme.R;
import pt.up.fe.aroundme.android.activities.LandmarkActivity;
import pt.up.fe.aroundme.android.activities.MapActivity;
import pt.up.fe.aroundme.android.exceptions.UserLocationIsNullException;
import pt.up.fe.aroundme.controllers.AroundMeController;
import pt.up.fe.aroundme.models.Landmark;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationManager;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;

public class MapManager {

    private static final int DEFAULT_RADIUS = 25;

    private final MapActivity mapActivity;

    private final SharedPreferences sharedPreferences;
    private final GoogleMap map;
    private final UserLocationManager userlocationManager;
    private final AroundMeController aroundmeController;

    private Integer radiusValue = DEFAULT_RADIUS;
    private Polygon radiusPolygon;
    private HashMap<Marker, Landmark> loadedMarkers;

    public MapManager(final MapActivity mapActivity) {
        this.mapActivity = mapActivity;

        this.map =
                ((SupportMapFragment) this.getMapActivity()
                        .getSupportFragmentManager().findFragmentById(R.id.map))
                        .getMap();

        this.map.setIndoorEnabled(true);
        this.map.setMyLocationEnabled(true);
        this.map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

            @Override
            public void onInfoWindowClick(final Marker marker) {
                final Intent intent =
                        new Intent(MapManager.this.mapActivity
                                .getApplicationContext(),
                                LandmarkActivity.class);
                intent.putExtra(LandmarkActivity.LANDMARK_ID_KEY,
                        MapManager.this.loadedMarkers.get(marker).getId());
                MapManager.this.mapActivity.startActivity(intent);

            }
        });
        this.map.animateCamera(CameraUpdateFactory
                .newCameraPosition(new CameraPosition.Builder().target(
                        new LatLng(41.1782628, -8.5947189)).zoom(12).build()),
                650, null);

        this.userlocationManager =
                new UserLocationManager(this, (LocationManager) this
                        .getMapActivity().getSystemService(
                                Context.LOCATION_SERVICE));

        this.aroundmeController = new AroundMeController(this);

        this.sharedPreferences =
                PreferenceManager.getDefaultSharedPreferences(this
                        .getMapActivity().getApplicationContext());
        this.loadedMarkers = new HashMap<Marker, Landmark>();

    }

    // MAP UPDATE METHODS

    public void update() throws UserLocationIsNullException {
        this.updateRadius();
        this.updateLandmarksMarkers();
    }

    public void updateRadius() throws UserLocationIsNullException {
        // TODO refactor radius_checkbox_key string
        if( !this.sharedPreferences.getBoolean("radius_checkbox_key", true) ) { return; }

        this.radiusValue =
                this.sharedPreferences.getInt("radius",
                        R.integer.radius_default);
        Log.d("updateRadius()", this.radiusValue + "");

        final PolygonOptions options = new PolygonOptions();

        // TODO refactor: create class with this method as static...dunno...
        final double R = 6371d; // earth's mean radius in km
        final double d = this.radiusValue / R; // radius given in km
        final double lat1 =
                Math.toRadians(this.userlocationManager.getLatitude());
        final double lon1 =
                Math.toRadians(this.userlocationManager.getLongitude());

        for(int x = 0; x <= 720; x++) {
            final double brng = Math.toRadians(x);
            final double latitudeRad =
                    Math.asin(Math.sin(lat1) * Math.cos(d) + Math.cos(lat1)
                            * Math.sin(d) * Math.cos(brng));
            final double longitudeRad =
                    (lon1 + Math.atan2(Math.sin(brng) * Math.sin(d)
                            * Math.cos(lat1), Math.cos(d) - Math.sin(lat1)
                            * Math.sin(latitudeRad)));
            options.add(new LatLng(Math.toDegrees(latitudeRad), Math
                    .toDegrees(longitudeRad)));
        }

        if( this.radiusPolygon != null ) {
            this.radiusPolygon.remove();
        }

        this.radiusPolygon =
                this.map.addPolygon(options.strokeColor(Color.LTGRAY)
                        .strokeWidth(2).fillColor(Color.LTGRAY));
    }

    public void updateLandmarksMarkers() {
        this.clearLandmarks();

        try {
            this.aroundmeController.refreshLandmarks(this.userlocationManager
                    .getLocation(), this.radiusValue);
        } catch (final UserLocationIsNullException e) {
            e.printStackTrace();
        }

        final List<Landmark> loadedLandmarks =
                this.aroundmeController.getLoadedLandmarks();
        Log.d("updateLandmarksMarkers()", this.loadedMarkers.size() + "");
        for(final Landmark landmark: loadedLandmarks) {
            this.addMarker(landmark);
        }
    }

    private void clearLandmarks() {

        final Set<Marker> loadedMarkers = this.loadedMarkers.keySet();

        for(final Marker marker: loadedMarkers) {
            Log.d("clearLandmarkMarkers()", "clearing...");
            marker.remove();
        }

        this.loadedMarkers = new HashMap<Marker, Landmark>();
    }

    public void toggleMapType(final View view) {
        this.map.setMapType(this.map.getMapType() == GoogleMap.MAP_TYPE_NORMAL
                ? GoogleMap.MAP_TYPE_HYBRID : GoogleMap.MAP_TYPE_NORMAL);
    }

    public void addMarker(final Landmark landmark) {
        if( this.loadedMarkers.containsValue(landmark) ) { return; }


    public Location getMyLocation() {
        return this.map.getMyLocation();
    }

}

我该如何解决这个问题?急需帮助。我该如何解决这个android运行时错误。我试过加载库和所有东西?

我已经尝试了所有现有解决方案,请检查一下。

4

4 回答 4

1
java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable

通常表示你的google-play-service库有问题,你引用它的方式有问题。

您可以查看我编写的本指南的前三个步骤:

谷歌地图 API V2

并确保你做对了。

于 2013-11-12T10:00:43.463 回答
0

如果您使用的是 gcm.jar,则可能是 jar 文件未正确压缩,或者您可以将 google play 服务库项目与您的项目一起使用并正确构建它。我希望,这会缩短您的问题。

于 2013-11-12T09:54:48.117 回答
0

NoClassDefFoundError 异常表示问题出在 Google Play 服务库的设置中,请确保您在您的项目中正确添加了 google_play_services设置 Google Play 服务 SDK

于 2013-11-12T09:55:05.027 回答
0

尝试在您的工作空间中播放 Google play 服务并尝试导入 google play 服务并将该 Google play 服务添加为您需要的项目,通过右键单击该项目转到属性--> Android --- >用图添加 Google play 服务

于 2013-11-12T10:00:14.603 回答