0

大家好,我正在使用此处给出的代码 在 Android 上获取用户当前位置的最简单、最可靠的方法是什么? 获取当前位置但我得到了ClassNotFoundExecption这是我的完整代码:

//地图活动代码:

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

}

在 MyLocation 中,我的代码与上面链接中给出的代码相同 这是我的 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
4

2 回答 2

2

您是否在 AndroidManifest.xml 中添加了上述代码?

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

您必须在应用程序标记之间添加此代码。

于 2012-10-18T12:06:32.510 回答
1

右键单击项目并转到属性->> java build path..单击库选项卡并单击添加外部 jar 并从您的计算机中选择 maps.jar...现在转到订购和导出选项卡并勾选复选框maps.jar ....现在...清理您的项目并运行...

于 2012-10-18T12:05:30.897 回答