在我的布局中添加MapView时出现ClassNotFoundException异常

3

我看到了这个帖子:

使用 MapView 抛出 ClassNotFoundException,但对我没有帮助。

我的清单文件中的代码如下:

<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>

我遇到了 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个回答

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>

谢谢回复,是的,我也尝试过这个方法,但仍然出现相同的错误。 - Shruti
请检查我的编辑答案,你必须将 uses-library 包含在 application 标签内。 - Lalit Poptani
@LalitPoptani,你觉得你应该提到我的答案,而不是编辑回答并使用相同的内容吗? - Paresh Mayani
不行,因为当时你的解决方案是不完整的,你现在才编辑了它。 - Lalit Poptani

2

请记住,您需要在<application>标签内声明<uses-library>标签。

<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>

谢谢回复,是的,我已经添加了它,下面是清单文件的完整代码。 - Shruti
请仔细阅读我的回答...你的AndroidManifest.xml文件仍然有问题,请检查我提到的内容。 - Paresh Mayani
已添加但仍然出现“classnotfoundexception”。 - Shruti

1

尝试更改清单文件为

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

不要使用 com.googlemaps.gpsapp.GoogleMapsGpsActivity,只需提及 GoogleMapsGpsActivity。不要加前缀的包名。 - Rashmi.B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接