0

我这里有一个典型的问题。我已经尝试成功加载 Android 地图,也尝试成功加载启动画面。

现在,当我尝试在启动画面后加载地图时,出现以下异常

04-08 00:35:46.890: E/AndroidRuntime(19891): FATAL EXCEPTION: main
04-08 00:35:46.890: E/AndroidRuntime(19891): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
04-08 00:35:46.890: E/AndroidRuntime(19891):    at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.app.Activity.onCreateView(Activity.java:4242)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.app.Activity.setContentView(Activity.java:1835)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at com.example.androidmapsv2.MainActivity.onCreate(MainActivity.java:53)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.app.Activity.performCreate(Activity.java:4465)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.os.Looper.loop(Looper.java:137)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at android.app.ActivityThread.main(ActivityThread.java:4424)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at java.lang.reflect.Method.invokeNative(Native Method)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at java.lang.reflect.Method.invoke(Method.java:511)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-08 00:35:46.890: E/AndroidRuntime(19891):    at dalvik.system.NativeStart.main(Native Method)
04-08 00:35:49.703: I/Process(19891): Sending signal. PID: 19891 SIG: 9

我已经按照所有基本程序嵌入地图和显示启动画面,因为当我尝试单独实现它们时成功了。

我已经确保 google-play-services jar 和 android-supportv4 jar 在 libs 文件夹中并且被正确引用。


MainActivity.java


package com.example.androidmapsv2;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
//import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
//import android.support.v4.app.FragmentActivity;
//import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnMapClickListener,
        OnMapLongClickListener, LocationListener {

    final int RQS_GooglePlayServices = 1;
    private GoogleMap myMap;

    Location myLocation;
    TextView tvLocInfo;
    LocationListener myLocationListener;

    double CURRENT_LAT = 0.0;
    double CURRENT_LON = 0.0;

    double PREVIOUS_LAT = 0.0;
    double PREVIOUS_LON = 0.0;

    LocationManager locationManager;
    String provider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvLocInfo = (TextView) findViewById(R.id.locinfo);

        FragmentManager myFragmentManager = getFragmentManager();
        MapFragment myMapFragment = (MapFragment) myFragmentManager
                .findFragmentById(R.id.map);
        myMap = myMapFragment.getMap();

        myMap.setMyLocationEnabled(true);

        // myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        // myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        // myMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

        // Getting LocationManager object
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        // Creating an empty criteria object
        Criteria criteria = new Criteria();

        // Getting the name of the provider that meets the criteria
        provider = locationManager.getBestProvider(criteria, false);

        if (provider != null && !provider.equals("")) {

            // Get the location from the given provider
            Location location = locationManager.getLastKnownLocation(provider);

            locationManager.requestLocationUpdates(provider, 20000, 1, this);

            if (location != null) {
                onLocationChanged(location);
            } else {
                Toast.makeText(getBaseContext(), "Location can't be retrieved",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(getBaseContext(), "No Provider Found",
                    Toast.LENGTH_SHORT).show();
        }

        myMap.setOnMapClickListener(this);
        myMap.setOnMapLongClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_legalnotices:
            String LicenseInfo = GooglePlayServicesUtil
                    .getOpenSourceSoftwareLicenseInfo(getApplicationContext());
            AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(
                    MainActivity.this);
            LicenseDialog.setTitle("Legal Notices");
            LicenseDialog.setMessage(LicenseInfo);
            LicenseDialog.show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        int resultCode = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getApplicationContext());

        if (resultCode == ConnectionResult.SUCCESS) {
            Toast.makeText(getApplicationContext(),
                    "isGooglePlayServicesAvailable SUCCESS", Toast.LENGTH_LONG)
                    .show();
        } else {
            GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    RQS_GooglePlayServices);
        }

    }

    @Override
    public void onMapClick(LatLng point) {
        tvLocInfo.setText(point.toString());
        myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
    }

    @Override
    public void onMapLongClick(LatLng point) {
        tvLocInfo.setText("New marker added@" + point.toString());
        myMap.addMarker(new MarkerOptions().position(point).title(
                point.toString()));
        // Polyline line =
//      myMap.addPolyline(new PolylineOptions().add(
//              new LatLng(12.97202903709172, 80.21577659994364),
//              new LatLng(12.978670181826473, 80.2174412459135))
//              .geodesic(true));

        double lat = point.latitude;
        double lon = point.longitude;

        System.out.println("lat = "+lat);

        CURRENT_LAT = lat;
        CURRENT_LON = lon;

        tvLocInfo.setText("lat: " + lat + "\n" + "lon: " + lon);

        LatLng latlng = new LatLng(lat,
                lon);

        myMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));

        if (CURRENT_LAT != 0.0 && CURRENT_LON != 0.0 && PREVIOUS_LAT != 0.0
                && PREVIOUS_LON != 0.0) {
            myMap.addPolyline(new PolylineOptions().add(
                    new LatLng(PREVIOUS_LAT, PREVIOUS_LON),
                    new LatLng(CURRENT_LAT, CURRENT_LON)).geodesic(true));
        }
        PREVIOUS_LAT = CURRENT_LAT;
        PREVIOUS_LON = CURRENT_LON;

    }

    @Override
    public void onLocationChanged(Location myLocation) {
        // TODO Auto-generated method stub
        // if (myLocationListener != null) {
        // myLocationListener.onLocationChanged(myLocation);

        double lat = myLocation.getLatitude();
        double lon = myLocation.getLongitude();

        CURRENT_LAT = lat;
        CURRENT_LON = lon;

        tvLocInfo.setText("lat: " + lat + "\n" + "lon: " + lon);

        LatLng latlng = new LatLng(myLocation.getLatitude(),
                myLocation.getLongitude());

        myMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));

        if (CURRENT_LAT != 0.0 && CURRENT_LON != 0.0 && PREVIOUS_LAT != 0.0
                && PREVIOUS_LON != 0.0) {
            myMap.addPolyline(new PolylineOptions().add(
                    new LatLng(PREVIOUS_LAT, PREVIOUS_LON),
                    new LatLng(CURRENT_LAT, CURRENT_LON)).geodesic(true));
        }
        PREVIOUS_LAT = CURRENT_LAT;
        PREVIOUS_LON = CURRENT_LON;

        // }
    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub
    }
}

闪屏.java

package com.example.androidmapsv2;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashScreen extends Activity {

    private static final int SPLASH_TIME = 3 * 1000;// 3 seconds

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {

                Intent intent = new Intent(SplashScreen.this,
                        MainActivity.class);
                startActivity(intent);

                SplashScreen.this.finish();

                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

            }
        }, SPLASH_TIME);

        new Handler().postDelayed(new Runnable() {
              @Override
              public void run() {
                     } 
                }, SPLASH_TIME);

    }


    @Override
    public void onBackPressed() {
        this.finish();
        super.onBackPressed();
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/locinfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment"/>

</LinearLayout>

闪屏.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:background="@drawable/splash"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidmapsv2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="16" />

    <permission
        android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" >
    </permission>

    <uses-permission android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAkfu2ZAgQfO6iQ_ldkOAaVyMOPDmq_Lg0" />

        <activity
            android:name="com.example.androidmapsv2.SplashScreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.androidmapsv2.MainActivity" >
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="My Key" />
    </application>

</manifest>

请看下面的结构

在此处输入图像描述

我还使用了最新的 API 密钥和其他基本的东西。

4

2 回答 2

0

对我来说,它看起来仍然是google-play-services引用的问题。

1.您能否确认您的red依赖项文件夹 ( green) 中有选定的 ( ) 文件。

在此处输入图像描述

2.请验证您的project.properties文件中是否有位于项目文件夹根目录中的这一行:

android.library.reference.1=../../android-sdk_r17-windows/extras/google/google_play_services/libproject/google-play-services_lib

或类似的东西。

于 2013-04-07T21:21:13.183 回答
0

以前版本的goolge地图已经贬值了,需要使用新的Api v2进行开发。

您已经在 Manifest.xml 中提供了两次密钥,现在将您的 MainActivity 扩展到 Fragment Activity 并替换

FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment = (MapFragment) myFragmentManager
        .findFragmentById(R.id.map);
myMap = myMapFragment.getMap();

经过 - -

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

并在 activity_main.xml 替换"com.google.android.gms.maps.MapFragment"

经过 - - -"com.google.android.gms.maps.SupportedMapFragment"

你很好。

于 2013-04-09T11:20:41.373 回答