我在 Eclipse 上开发这个。我的应用程序旨在使用 GPS 查找当前位置,但是当我在模拟器上运行它时,它显示出意外的强制关闭。我附上了 main.xml、java 文件和 manifest.xml。
主文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".GooglemapsActivity" >
<com.google.android.maps.MapView
android:id="@+id/mapview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0o6t2Gs5-C0eAHL2ZNbwvzu4pwsgX50HX2X8rKA"
/>
</RelativeLayout>
Java 文件:
package com.example.mapgp;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import com.example.mapgp.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import android.widget.Toast;
public class Gmapwithgps extends MapActivity {
public MapView mapView;
public MapController mc;
public MyLocationListener locl;
public LocationManager manager;
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +
"Lati = " + loc.getLatitude() +
"Longi = " + loc.getLongitude();
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gmapwithgps);
mapView = (MapView) findViewById(R.id.mapview1);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locl = new MyLocationListener();
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locl);
}
protected boolean isRouteDisplayed() {
return false;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_gmapwithgps, menu);
return true;
}
}
清单.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapgp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps"/>
<activity
android:name="com.example.mapgp.GmapwithgpsActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
日志猫:
01-04 22:36:54.120: D/AndroidRuntime(1510): Shutting down VM
01-04 22:36:54.120: W/dalvikvm(1510): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-04 22:36:54.251: E/AndroidRuntime(1510): FATAL EXCEPTION: main
01-04 22:36:54.251: E/AndroidRuntime(1510): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mapgp/com.example.mapgp.GmapwithgpsActivity}: java.lang.ClassNotFoundException: com.example.mapgp.GmapwithgpsActivity in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.example.mapgp-1.apk]
01-04 22:36:54.251: E/AndroidRuntime(1510): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
01-04 22:36:54.251: E/AndroidRuntime(1510): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-04 22:36:54.251: E/AndroidRuntime(1510): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-04 22:36:54.251: E/AndroidRuntime(1510): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-04 22:36:54.251: E/AndroidRuntime(1510): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 22:36:54.251: E/AndroidRuntime(1510): at android.os.Looper.loop(Looper.java:130)
01-04 22:36:54.251: E/AndroidRuntime(1510): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-04 22:36:54.251: E/AndroidRuntime(1510): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 22:36:54.251: E/AndroidRuntime(1510): at java.lang.reflect.Method.invoke(Method.java:507)
01-04 22:36:54.251: E/AndroidRuntime(1510): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-04 22:36:54.251: E/AndroidRuntime(1510): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-04 22:36:54.251: E/AndroidRuntime(1510): at dalvik.system.NativeStart.main(Native Method)
01-04 22:36:54.251: E/AndroidRuntime(1510): Caused by: java.lang.ClassNotFoundException: com.example.mapgp.GmapwithgpsActivity in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.example.mapgp-1.apk]
01-04 22:36:54.251: E/AndroidRuntime(1510): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
01-04 22:36:54.251: E/AndroidRuntime(1510): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
01-04 22:36:54.251: E/AndroidRuntime(1510): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-04 22:36:54.251: E/AndroidRuntime(1510): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-04 22:36:54.251: E/AndroidRuntime(1510): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
01-04 22:36:54.251: E/AndroidRuntime(1510): ... 11 more
01-04 22:37:24.730: I/Process(1510): Sending signal. PID: 1510 SIG: 9