0

我尝试了几次解决这个问题,我无法按照指南
https://developers.google.com/maps/documentation/android/start

但我无法解决这个问题任何人都知道这个错误请帮助我

这是活动>

package com.example.lifecycle;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends android.support.v4.app.FragmentActivity {

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

}

@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;
}

}

这是 Xml

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
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"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

<fragment  
 android:id="@+id/fragment1"  
 android:layout_width="match_parent"  
 android:layout_height="match_parent"  
 class="com.google.android.gms.maps.MapFragment"
 android:name="com.testing.svma.MainActivity" /> 

</RelativeLayout>

——这很明显——

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

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

<permission
    android:name="com.example.lifecycle.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="com.example.lifecycle.permission.MAPS_RECEIVE"/>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission   android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>            
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<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="xxxxxxxxxxxxxxxxx" /> 

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

I also reference my app to google-play-services_lib in eclipse.

but everytime, error report in logcat like this:

     06-06 08:46:06.513: E/Trace(1136): error opening trace file: No such file or  directory (2)
     06-06 08:46:07.411: E/AndroidRuntime(1136): FATAL EXCEPTION: main
     06-06 08:46:07.411: E/AndroidRuntime(1136): java.lang.RuntimeException: Unable to  instantiate activity  ComponentInfo{com.example.lifecycle/com.example.lifecycle.MainActivity}:  java.lang.ClassNotFoundException: Didn't find class "com.example.lifecycle.MainActivity" on  path: /data/app/com.example.lifecycle-2.apk
     06-06 08:46:07.411: E/AndroidRuntime(1136):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
     06-06 08:46:07.411: E/AndroidRuntime(1136):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
     06-06 08:46:07.411: E/AndroidRuntime(1136):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
     06-06 08:46:07.411: E/AndroidRuntime(1136):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    06-06 08:46:07.411: E/AndroidRuntime(1136):     at android.os.Handler.dispatchMessage(Handler.java:99)
    06-06 08:46:07.411: E/AndroidRuntime(1136):     at android.os.Looper.loop(Looper.java:137)
    06-06 08:46:07.411: E/AndroidRuntime(1136):     at android.app.ActivityThread.main(ActivityThread.java:5041)
    06-06 08:46:07.411: E/AndroidRuntime(1136):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-06 08:46:07.411: E/AndroidRuntime(1136):     at java.lang.reflect.Method.invoke(Method.java:511)
    06-06 08:46:07.411: E/AndroidRuntime(1136):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    06-06 08:46:07.411: E/AndroidRuntime(1136):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    06-06 08:46:07.411: E/AndroidRuntime(1136):     at dalvik.system.NativeStart.main(Native Method)
    06-06 08:46:07.411: E/AndroidRuntime(1136): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.lifecycle.MainActivity" on path: /data/app/com.example.lifecycle-2.apk
    06-06 08:46:07.411: E/AndroidRuntime(1136):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
    06-06 08:46:07.411: E/AndroidRuntime(1136):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    06-06 08:46:07.411: E/AndroidRuntime(1136):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    06-06 08:46:07.411: E/AndroidRuntime(1136):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
    06-06 08:46:07.411: E/AndroidRuntime(1136):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
    06-06 08:46:07.411: E/AndroidRuntime(1136):     ... 11 more

谢谢你 -

4

1 回答 1

2

Google Map V2 不支持模拟器进行测试。你需要一个设备来测试它。你在设备上测试过吗?

这是我的工作代码试试这个:清单文件:

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

            <uses-sdk
                android:minSdkVersion="8"
                android:targetSdkVersion="17" />

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

            <uses-permission android:name="com.example.lastgmap.permission.MAPS_RECEIVE" />
            <uses-permission android:name="android.permission.INTERNET" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
            <uses-permission       android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
            <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" >
                <activity
                    android:name="com.example.lastgmap.MainActivity"
                    android:label="@string/app_name" >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />

                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>

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

        </manifest>

我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/locinfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <fragment

            android:name="com.google.android.gms.maps.SupportMapFragment"

            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

JAVA 文件(主活动):

 package com.example.lastgmap;
            import android.graphics.Color;
            import android.location.Criteria;
            import android.location.Location;
            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.widget.TextView;
            import android.widget.Toast;

            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.OnInfoWindowClickListener;
            import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
            import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
            import com.google.android.gms.maps.SupportMapFragment;
            import com.google.android.gms.maps.model.BitmapDescriptorFactory;
            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.Polyline;
            import com.google.android.gms.maps.model.PolylineOptions;

            public class MainActivity extends FragmentActivity implements
                    OnMapClickListener, OnMapLongClickListener, OnInfoWindowClickListener {

                final int RQS_GooglePlayServices = 1;
                GoogleMap myMap;
                TextView tvLocInfo;

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

                    tvLocInfo = (TextView) findViewById(R.id.locinfo);
                    FragmentManager myFragmentManager = getSupportFragmentManager();
                    SupportMapFragment mySupportMapFragment = (SupportMapFragment) myFragmentManager
                            .findFragmentById(R.id.map);

                    myMap = mySupportMapFragment.getMap();
                    myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                    myMap.setMyLocationEnabled(true);

                    // myMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

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

                    LatLng latlan = new LatLng(17.901126806011973, 74.66820292174816);
                    myMap.addMarker(new MarkerOptions().position(latlan).title("suraj"));
                    myMap.setMyLocationEnabled(true);

                    setUpMap();

                    Polyline line = myMap.addPolyline(new PolylineOptions()
                    .add(new LatLng(18.81319, 73.96298), new LatLng(18.95285, 73.85734))
                    .width(25)
                    .color(Color.BLUE)
                    .geodesic(true));

                    LatLng p=new LatLng(18.95285, 73.85734);
                    myMap.animateCamera(CameraUpdateFactory.newLatLng(p));
                }

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

                @Override
                public void onMapClick(LatLng point) {
                    // TODO Auto-generated method stub

                    tvLocInfo.setText(point.toString());
                    myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
                }

                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 onMapLongClick(LatLng point) {
                    // TODO Auto-generated method stub

                    tvLocInfo.setText("new marker added @" + point.toString());
                    // myMap.addMarker(new
                    // MarkerOptions().position(point).title(point.toString()));

                    // Marker melbourne = mMap.addMarker(new MarkerOptions()
                    // .position(MELBOURNE)
                    // .title("Melbourne")
                    // .snippet("Population: 4,137,400")
                    // .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

                    Marker m = myMap.addMarker(new MarkerOptions().position(point)
                            .title("Custome").snippet("Population:1321231")
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));

                }

                private void setUpMap() {
                    // Enable MyLocation Layer of Google Map
                    myMap.setMyLocationEnabled(true);

                    // Get LocationManager object from System Service LOCATION_SERVICE
                    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

                    // Create a criteria object to retrieve provider
                    Criteria criteria = new Criteria();

                    // Get the name of the best provider
                    String provider = locationManager.getBestProvider(criteria, true);

                    // Get Current Location
                    Location myLocation = locationManager.getLastKnownLocation(provider);

                    // set map type
                    myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

                    // Get latitude of the current location
                    double latitude = myLocation.getLatitude();

                    // Get longitude of the current location
                    double longitude = myLocation.getLongitude();

                    // Create a LatLng object for the current location
                    LatLng latLng = new LatLng(latitude, longitude);

                    // Show the current location in Google Map
                    myMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

                    // Zoom in the Google Map
                    myMap.animateCamera(CameraUpdateFactory.zoomTo(20));
                    myMap.addMarker(new MarkerOptions().position(
                            new LatLng(latitude, longitude)).title("You are here!"));
                }

                @Override
                public void onInfoWindowClick(Marker marker) {
                    // TODO Auto-generated method stub
                    marker.showInfoWindow();
                }

            }
于 2013-06-06T10:02:08.607 回答