找不到类异常Android

时间:2011-03-31 16:10:38

标签: android android-intent

我正在使用两个单独的类,其中一个有一些按钮,另一个打开谷歌地图,我正在做一个叠加。如果有人能看到我打算打开Map.class的问题,请告诉我。我将抛出我的错误消息和代码。

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)

7 个答案:

答案 0 :(得分:12)

这可能是概率顺序的下列原因之一(因此稀有的逆序)

  1. AndroidManifest.xml问题。 <activity>部分中的<uses-library android:name="com.google.android.maps" />代码,<application>声明
  2. 安装问题:手动添加maps.jar文件以及设置项目类型以使用Google API。由于某种原因,这会引起问题。
  3. 最后,您调用activity可能会继承一些导致此问题的类。从使用droidFu API的类调用MapActivity时遇到了同样的问题。调用活动使用了droidFu的BetterActivity继承。在我从droidFu API将MapActivity更改为BetterMapActivity之后,我才能使地图生效。

答案 1 :(得分:4)

遇到同样的问题,并通过在应用程序标签

之间将其添加到AndroidManifest.xml中来解决
<application
       ...
       ... >
**<uses-library android:name="com.google.android.maps" />**
<activity .... />
</application>

答案 2 :(得分:2)

您是否获得了Google API地图密钥并在R.layout.map中指定了该密钥?另外,请务必允许清单中的权限访问互联网。

答案 3 :(得分:0)

您的AndroidManifest.xml中的Map活动是否有<activity/>元素?

答案 4 :(得分:0)

查看Google Maps Android API gives a NoClassDefFoundErrorNoClassDefFoundError in mapActivity中的一些答案,以涵盖大多数可能性。

答案 5 :(得分:0)

将cbHome类扩展为MapActivity

答案 6 :(得分:0)

如果你在某些活动中,并且你想显示一个地图并且它给出了上面提到的错误,那么它是由于缺少 android.manifest 中的这行代码,所以把这段代码可以解决错误。

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