Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Classnotfoundexception on adding mapview in my layout

I have seen this thread

ClassNotFoundException Thrown using MapView but no help for me.

The code im my manifest file is :

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

and my code for GoogleMapsGpsActivity.java is :

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>

and I am getting ClassNotFoundException as can be seen from following logcat output

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

Please point out my mistake

Edit

Thank u everybody for their responses the error was in my main.xml in declaring mapview

it should be like

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

instead of the one posted above.

like image 517
Shruti Avatar asked Jun 30 '26 09:06

Shruti


2 Answers

Just keep in mind that you need to declare <uses-library> tag inside the <application> tag.

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

Final soluton:

<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>
like image 156
Paresh Mayani Avatar answered Jul 02 '26 23:07

Paresh Mayani


Try like this,

 <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>
like image 33
Lalit Poptani Avatar answered Jul 03 '26 00:07

Lalit Poptani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!