在我的布局中添加mapview的Classnotfoundexception

时间:2012-01-02 07:16:18

标签: android gps android-mapview classnotfoundexception

我见过这个帖子

ClassNotFoundException Thrown using MapView 但对我没有帮助。

我的清单文件中的代码是:

<uses-sdk android:minSdkVersion="10" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />

<uses-library android:name="com.google.android.maps" >
</uses-library>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".GoogleMapsGpsActivity"
        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>

我的 GoogleMapsGpsActivity.java 的代码是:

public class GoogleMapsGpsActivity extends MapActivity {
    private LocationManager lm;
    private LocationListener locationListener;

    private MapView mapView;
    private MapController mc;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 

        //---use the LocationManager class to obtain GPS locations---
        lm = (LocationManager) 
            getSystemService(Context.LOCATION_SERVICE);    

        locationListener = new MyLocationListener();

        lm.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);

        mapView = (MapView) findViewById(R.id.mapview);
        mc = mapView.getController();

    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }        

    private class MyLocationListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location loc) {
            if (loc != null) {                
                Toast.makeText(getBaseContext(), 
                    "Location changed : Lat: " + loc.getLatitude() + 
                    " Lng: " + loc.getLongitude(), 
                    5000).show();

                GeoPoint p = new GeoPoint(
                        (int) (loc.getLatitude() * 1E6), 
                        (int) (loc.getLongitude() * 1E6));
                mc.animateTo(p);
                mc.setZoom(16);                
                mapView.invalidate();
            }
        }

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

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

        @Override
        public void onStatusChanged(String provider, int status, 
            Bundle extras) {
            // TODO Auto-generated method stub
        }
    }  
}

main.xml中:

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

    <com.googlemaps.gpsapp.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="key"
    />
</LinearLayout>

我从{log}输出

可以看到ClassNotFoundException
1-02 12:44:17.156: E/AndroidRuntime(1585): FATAL EXCEPTION: main
01-02 12:44:17.156: E/AndroidRuntime(1585): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.googlemaps.gpsapp/com.googlemaps.gpsapp.GoogleMapsGpsActivity}: java.lang.ClassNotFoundException: com.googlemaps.gpsapp.GoogleMapsGpsActivity in loader dalvik.system.PathClassLoader[/data/app/com.googlemaps.gpsapp-2.apk]
01-02 12:44:17.156: E/AndroidRuntime(1585):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at android.os.Looper.loop(Looper.java:130)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at java.lang.reflect.Method.invokeNative(Native Method)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at java.lang.reflect.Method.invoke(Method.java:507)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at dalvik.system.NativeStart.main(Native Method)
01-02 12:44:17.156: E/AndroidRuntime(1585): Caused by: java.lang.ClassNotFoundException: com.googlemaps.gpsapp.GoogleMapsGpsActivity in loader dalvik.system.PathClassLoader[/data/app/com.googlemaps.gpsapp-2.apk]
01-02 12:44:17.156: E/AndroidRuntime(1585):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-02 12:44:17.156: E/AndroidRuntime(1585):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
01-02 12:44:17.156: E/AndroidRuntime(1585):     ... 11 more

请指出我的错误

修改

感谢大家的回复,错误是在我的main.xml中声明mapview

它应该像

<com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="key"
    />

而不是上面发布的那个。

3 个答案:

答案 0 :(得分:2)

试试这个,

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.googlemaps.gpsapp" android:versionCode="1"
    android:versionName="1.0">
    ...
       <application ...>
          <uses-library android:name="com.google.android.maps" />
          <activity
        android:name=".GoogleMapsGpsActivity"
        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>
    </manifest>

答案 1 :(得分:2)

请注意,您需要在<uses-library>代码中声明<application>代码。

<application>
    ........
    <uses-library android:name="com.google.android.maps">
    <activity>  
       ......
       ......
    </activity>
</application>

最终解决方案:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

<uses-library android:name="com.google.android.maps" > </uses-library>
// This should be here

    <activity
        android:name=".GoogleMapsGpsActivity"
        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>

答案 2 :(得分:1)

尝试将清单文件更改为

        activity android:name=".GoogleMapsGpsActivity"
              android:label="@string/app_name"
       intent-filter
            action android:name="android.intent.action.MAIN" /
       /intent-filter
    /activity