2011-03-31 141 views
4

我正在使用兩個獨立的類之一,其中一個按鈕,另一個打開谷歌地圖,我正在做一個覆蓋。如果任何人都可以看到我的意圖打開Map.class的問題讓我知道。我會拋出我的錯誤消息和代碼。找不到類異常Android

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) 
+0

您的com.state.park.Map類是在您的AndroidManifest.xml文件中聲明的嗎? – rajath 2011-03-31 16:20:31

回答

0

你有你的AndroidManifest.xml您的地圖活動的<activity/>元素?

+0

是的。 2011-03-31 16:21:11

+0

只是在黑暗中拍攝的一個鏡頭,但放在清單中的''和''內的活動是? – whirlwin 2011-03-31 16:26:32

2

您是否獲得Google API映射鍵並在R.layout.map中指定它?另外請確保允許清單中的權限訪問互聯網。

+0

是的。我確實有api密鑰,因特網訪問能夠在之前運行地圖。 之內 2011-03-31 16:29:23

+2

是否在之間的清單中添加了? – DRiFTy 2011-03-31 16:32:25

12

它可以是概率(和稀有因此相反的順序)

  1. 的AndroidManifest.xml問題的順序的下列原因之一。 <activity>標記,<uses-library android:name="com.google.android.maps" /><application>部分中的聲明等
  2. 設置問題:手動添加maps.jar文件以及設置項目類型以使用Google API。由於某些原因,這會導致問題。
  3. 最後,您調用活動可能會繼承導致此問題的某個類。從使用droidFu API的類調用MapActivity時,我遇到了同樣的問題。調用活動使用了droidFu的BetterActivity繼承。在將MapActivity從droidFu API更改爲BetterMapActivity後,我只獲得了地圖的工作。
+0

此外,一些類的用法應與繼承一起考慮。當嘗試啓動一個使用某些API(PopupMenu)不適用於舊版Android版本的活動時,我得到了'NoClassDefFoundError'。 – 2013-04-10 18:44:11

0

的cbHome類擴展到MapActivity

4

有同樣的問題和應用標籤之間加入這個解決到AndroidManifest.xml中

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

如果你在一些地方活動,要顯示地圖,它給出了上面提到的錯誤,那麼這是由於這行代碼丟失在android.manifest,所以把這個代碼,並得到一個錯誤騎。

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