Cory Dawson
Cory Dawson

Reputation: 81

Could not find Class exception Android

I am working with two seperate classes one of which has some buttons and the other opens google maps and I am doing an overlay on it. If anyone could see the problem with my intent to open the Map.class let me know. I will throw in my error messages and code.

package com.state.park;




import java.util.ArrayList;
import java.util.List;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.widget.LinearLayout;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Projection;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Map extends MapActivity {

    private MapView mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.map);

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

        mapView.setBuiltInZoomControls(true);

        mapView.setClickable(true);

        Drawable marker = getResources().getDrawable(R.drawable.icon);
        marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());

        InterestingLocations funPlaces = new InterestingLocations(marker);
        mapView.getOverlays().add(funPlaces);
        GeoPoint pt = funPlaces.getCenter();
        mapView.getController().setCenter(pt);
        mapView.getController().setZoom(15);
    }

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

    class InterestingLocations extends ItemizedOverlay{

        private List<OverlayItem> locations = new ArrayList<OverlayItem>();

        private Drawable marker;
        private GeoPoint p1, p2 ,p3;
        private Paint paint;
        public InterestingLocations(Drawable defaultMarker) {
            super(defaultMarker);

            marker = defaultMarker;

             p1 = new GeoPoint((int)(34.044125 * 1000000) , (int)(-77.912636 * 1000000));
             p2 = new GeoPoint((int)(34.046544 * 1000000) , (int) (-77.918043 * 1000000));
             p3 = new GeoPoint((int) (34.041992 * 1000000) , (int)(-77.921476 * 1000000));



            locations.add(new OverlayItem(p1 , "special1" , "special1"));
            locations.add(new OverlayItem(p2 , "special2" , "special12"));
            locations.add(new OverlayItem(p3 , "special3" , "special3"));
            // TODO Auto-generated constructor stub
            populate();
        }

        public void draw(Canvas canvas, MapView mapView , boolean shadow){
             super.draw(canvas, mapView, shadow);

             boundCenterBottom(marker);

             Point from = new Point();
             Point to = new Point();

             paint = new Paint();

                paint.setColor(Color.BLACK);
                paint.setStrokeWidth(2);
                paint.setStyle(Style.FILL);

             Path path = new Path();        
              Projection proj = mapView.getProjection();       
              proj.toPixels(p1,from );       
              proj.toPixels(p2, to);     
              path.moveTo(from.x, from.y);     
              path.lineTo(to.x,to.y);      
              canvas.drawLine(from.x, from.y, to.x, to.y, paint); 



        }

        @Override
        protected OverlayItem createItem(int i) {
            // TODO Auto-generated method stub
            return locations.get(i);
        }

        @Override
        public int size() {
            // TODO Auto-generated method stub
            return locations.size();
        }   


    }
}

package com.state.park;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

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

        Button POI = (Button)findViewById(R.id.poi);

        POI.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent i = new Intent(CBHome.this , Map.class);

                CBHome.this.startActivity(i);

            }



        });

    }



}

03-31 17:15:51.882: ERROR/dalvikvm(1033): Could not find class 'com.state.park.Map', referenced from method com.state.park.CBHome$1.onClick
03-31 17:15:55.133: ERROR/AndroidRuntime(1033): FATAL EXCEPTION: main
03-31 17:15:55.133: ERROR/AndroidRuntime(1033): java.lang.NoClassDefFoundError: com.state.park.Map
03-31 17:15:55.133: ERROR/AndroidRuntime(1033):     at com.state.park.CBHome$1.onClick(CBHome.java:25)
03-31 17:15:55.133: ERROR/AndroidRuntime(1033):     at android.view.View.performClick(View.java:2408)
03-31 17:15:55.133: ERROR/AndroidRuntime(1033):     at android.view.View$PerformClick.run(View.java:8816)
03-31 17:15:55.133: ERROR/AndroidRuntime(1033):     at android.os.Handler.handleCallback(Handler.java:587)
03-31 17:15:55.133: ERROR/AndroidRuntime(1033):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-31 17:15:55.133: ERROR/AndroidRuntime(1033):     at android.os.Looper.loop(Looper.java:123)
03-31 17:15:55.133: ERROR/AndroidRuntime(1033):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-31 17:15:55.133: ERROR/AndroidRuntime(1033):     at java.lang.reflect.Method.invokeNative(Native Method)
03-31 17:15:55.133: ERROR/AndroidRuntime(1033):     at java.lang.reflect.Method.invoke(Method.java:521)
03-31 17:15:55.133: ERROR/AndroidRuntime(1033):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-31 17:15:55.133: ERROR/AndroidRuntime(1033):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-31 17:15:55.133: ERROR/AndroidRuntime(1033):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 4

Views: 27209

Answers (7)

Pir Fahim Shah
Pir Fahim Shah

Reputation: 10623

If you are in some where activity and you want to display a map and it gives the above mentioned error, then it is due to missing of this line of code in android.manifest, so put this code and get ride from an error.

     <application
           .......
     <uses-library android:name="com.google.android.maps" /> //this is your required code
     <activity
        .....           
     </activity>
    </application>

Upvotes: 0

mtekeli
mtekeli

Reputation: 705

Had the same problem and solved by adding this into AndroidManifest.xml between application tags

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

Upvotes: 4

user1464543
user1464543

Reputation:

extend the cbHome class to MapActivity

Upvotes: 0

Saad Farooq
Saad Farooq

Reputation: 13402

It may be one of the following reasons in order of probability (and therefore inverse order of rarity)

  1. AndroidManifest.xml problems. <activity> tags, <uses-library android:name="com.google.android.maps" /> declaration in the <application> section, etc.
  2. Setup problems: adding the maps.jar files manually as well as setting the project type to use Google APIs. This cause problems for some reason.
  3. Finally, you calling activity may be inheriting some class that causes this problem. I had the same problem calling a MapActivity from a class that used the droidFu API. The calling activity used droidFu's BetterActivity inheritance. I only got the map to work after I changed the MapActivity to BetterMapActivity from the droidFu API.

Upvotes: 12

Rajath
Rajath

Reputation: 11926

Take a look at some of the answers in Google Maps Android API gives a NoClassDefFoundError and NoClassDefFoundError in mapActivity to cover most possibilities.

Upvotes: 0

DRiFTy
DRiFTy

Reputation: 11369

Did you get a Google API map key and specify it in R.layout.map? Also be sure to allow permissions in the manifest to access the internet.

Upvotes: 1

Matthew
Matthew

Reputation: 44919

Do you have an <activity/> element for your Map activity in your AndroidManifest.xml?

Upvotes: 0

Related Questions