2012-10-18 103 views
0

大家好,我使用這裏給出What is the simplest and most robust way to get the user's current location on Android?代碼即可獲得當前位置,但我得到了ClassNotFoundExecption這是我的完整代碼:無法解決ClassNotFoundException的問題?

// MapActivity代碼:

package com.example.gmaps; 
import com.example.gmaps.MyLocation.LocationResult; 
import com.google.android.maps.MapActivity; 
import android.location.Location; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.Toast; 

public class MainActivity extends MapActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    LocationResult locationResult = new LocationResult(){ 
     @Override 
     public void gotLocation(Location location){ 
      //Got the location! 

      Toast.makeText(getApplicationContext(), "I got the location", Toast.LENGTH_LONG).show(); 
     } 
    }; 
    MyLocation myLocation = new MyLocation(); 
    myLocation.getLocation(MainActivity.this, locationResult); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

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

}

和在我的位置我有相同的代碼,如上面鏈接 這裏是我的logcat

> 10-18 16:49:51.222: E/AndroidRuntime(24816): FATAL EXCEPTION: main 
10-18 16:49:51.222: E/AndroidRuntime(24816): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.gmaps/com.example.gmaps.MainActivity}: java.lang.ClassNotFoundException: com.example.gmaps.MainActivity 
10-18 16:49:51.222: E/AndroidRuntime(24816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1993) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at android.app.ActivityThread.access$600(ActivityThread.java:132) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at android.os.Looper.loop(Looper.java:137) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at android.app.ActivityThread.main(ActivityThread.java:4575) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at java.lang.reflect.Method.invokeNative(Native Method) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at java.lang.reflect.Method.invoke(Method.java:511) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at dalvik.system.NativeStart.main(Native Method) 
10-18 16:49:51.222: E/AndroidRuntime(24816): Caused by: java.lang.ClassNotFoundException: com.example.gmaps.MainActivity 
10-18 16:49:51.222: E/AndroidRuntime(24816): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at android.app.Instrumentation.newActivity(Instrumentation.java:1023) 
10-18 16:49:51.222: E/AndroidRuntime(24816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1984) 
10-18 16:49:51.222: E/AndroidRuntime(24816): ... 11 more 
+0

我自己也嘗試從計算器許多解決方案,但未能解決這個異常 –

+0

你在模擬器中運行..使仿真器是谷歌SDK模擬器 – Sandy09

+0

並選擇谷歌Android SDK版本.. – Sandy09

回答

2

你在AndroidManifest.xml中添加了上面的代碼嗎?

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

您必須在應用程序標籤之間添加此代碼。

+0

是的,我忘了添加這個非常感謝。 BUt我無法得到我目前的位置。例外是沒有更多的,我接受你的答案你會幫我爲什麼我無法獲得我的位置? –

+0

爲此,我建議您使用vogella網站(http://www.vogella.com/articles/AndroidLocationAPI/article.html,位於4.3)將其應用於您的項目。然後你可以得到你的位置 –

+0

謝謝。我會盡力告訴你 –

1

右鍵單擊Project並轉到屬性 - >> java build path ..點擊Libraries選項卡,然後單擊Add External jar並從您的計算機中選擇maps.jar ...現在轉到命令並導出選項卡並勾選複選框maps.jar ....現在...清理你的項目並運行...