打开Goog​​le地图时强制关闭

时间:2012-02-14 12:14:39

标签: android google-maps android-manifest

我知道他们是一些帖子,也有同样的问题。 我已经读完了所有这些,但这并没有解决我的问题。这是我的AndroidManifest.XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.first.project"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>   

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <uses-library android:name="com.google.android.maps" />

    <activity
        android:name=".GPS"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

    <activity android:name=".About"
              android:label="@string/about_title" />
    <activity android:name=".getGPS"
              android:label="@string/get_gps" />
</application>
</manifest>

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/id1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My text edit this."
/>
<com.google.android.maps.MapView
android:id="@+id/myGMap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0VeISgtB_USJi-BUVme-Zmbo7st7ED2Pf3TAv**"
/>
</LinearLayout>

这是日志:

02-14 20:53:06.077: E/AndroidRuntime(302): FATAL EXCEPTION: main
02-14 20:53:06.077: E/AndroidRuntime(302): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.first.project/my.first.project.getGPS}: android.view.InflateException: Binary XML file line #12: Error inflating class com.google.android.maps.MapView
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.os.Looper.loop(Looper.java:123)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-14 20:53:06.077: E/AndroidRuntime(302):  at java.lang.reflect.Method.invokeNative(Native Method)
02-14 20:53:06.077: E/AndroidRuntime(302):  at java.lang.reflect.Method.invoke(Method.java:521)
02-14 20:53:06.077: E/AndroidRuntime(302):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-14 20:53:06.077: E/AndroidRuntime(302):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-14 20:53:06.077: E/AndroidRuntime(302):  at dalvik.system.NativeStart.main(Native Method)
02-14 20:53:06.077: E/AndroidRuntime(302): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.google.android.maps.MapView
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.view.LayoutInflater.createView(LayoutInflater.java:513)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-14 20:53:06.077: E/AndroidRuntime(302):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.app.Activity.setContentView(Activity.java:1647)
02-14 20:53:06.077: E/AndroidRuntime(302):  at my.first.project.getGPS.onCreate(getGPS.java:45)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-14 20:53:06.077: E/AndroidRuntime(302):  ... 11 more
02-14 20:53:06.077: E/AndroidRuntime(302): Caused by: java.lang.reflect.InvocationTargetException
02-14 20:53:06.077: E/AndroidRuntime(302):  at com.google.android.maps.MapView.<init>(MapView.java:238)
02-14 20:53:06.077: E/AndroidRuntime(302):  at java.lang.reflect.Constructor.constructNative(Native Method)
02-14 20:53:06.077: E/AndroidRuntime(302):  at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
02-14 20:53:06.077: E/AndroidRuntime(302):  at android.view.LayoutInflater.createView(LayoutInflater.java:500)
02-14 20:53:06.077: E/AndroidRuntime(302):  ... 21 more
02-14 20:53:06.077: E/AndroidRuntime(302): Caused by: java.lang.NullPointerException
02-14 20:53:06.077: E/AndroidRuntime(302):  at com.google.android.maps.MapActivity.setupMapView(MapActivity.java:382)
02-14 20:53:06.077: E/AndroidRuntime(302):  at com.google.android.maps.MapView.<init>(MapView.java:280)
02-14 20:53:06.077: E/AndroidRuntime(302):  at com.google.android.maps.MapView.<init>(MapView.java:255)
02-14 20:53:06.077: E/AndroidRuntime(302):  ... 25 more

我收到“强制关闭”警告。

有什么建议吗?


public class getGPS extends MapActivity implements LocationListener 
{    
EditText txted = null;
Button btnSimple = null;
MapView gMapView = null;
MapController mc = null;
Drawable defaultMarker = null;
GeoPoint p = null;
double latitude = 10, longitude = 10;

@Override
public void onCreate(Bundle savedInstanceState)
{
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.getgps);

    // Creating TextBox displying Lat, Long
    txted = (EditText) findViewById(R.id.id1);
    String currentLocation = "Lat: " + latitude + " Lng: " + longitude;
    txted.setText(currentLocation);

    // Creating and initializing Map
    gMapView = (MapView) findViewById(R.id.myGMap);
    p = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
    gMapView.setSatellite(true);
    mc = gMapView.getController();
    mc.setCenter(p);
    mc.setZoom(14);

    // Add a location mark
    MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
    List<Overlay> list = gMapView.getOverlays();
    list.add(myLocationOverlay);

    // Adding zoom controls t o Map
    @SuppressWarnings("deprecation")
    ZoomControls zoomControls = (ZoomControls) gMapView.getZoomControls();
    zoomControls.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

    gMapView.addView(zoomControls);
    gMapView.displayZoomControls(true);

    // Getting locationManager and reflecting changes over map if distance travel by
    // user is greater than 500m from current location.
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
}

/* This method is called when use position will get changed */
public void onLocationChanged(Location location) {
    if (location != null) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        String currentLocation = "Lat: " + lat + " Lng: " + lng;
        txted.setText(currentLocation);
        p = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
        mc.animateTo(p);
    }
}

public void onProviderDisabled(String provider) {
    // required for interface, not used
}

public void onProviderEnabled(String provider) {
    // required for interface, not used
}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // required for interface, not used
}

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

/* Class overload draw method which actually plot a marker,text etc. on Map */
protected class MyLocationOverlay extends com.google.android.maps.Overlay {

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
    {
        Paint paint = new Paint();

        super.draw(canvas, mapView, shadow);
        // Converts lat/lng-Point to OUR coordinates on the screen.
        Point myScreenCoords = new Point();
        mapView.getProjection().toPixels(p, myScreenCoords);

        paint.setStrokeWidth(1);
        paint.setARGB(255, 255, 255, 255);
        paint.setStyle(Paint.Style.STROKE);

        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);

        canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
        canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint);
        return true;
    }
}
}

3 个答案:

答案 0 :(得分:0)

你可以在视图中尝试这样的地图

<view android:id="@+id/mapview"
 class="com.google.android.maps.MapView"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:layout_weight="1" 
 android:clickable="true"
 android:apiKey="key"
/> 

我认为它的观点问题。

答案 1 :(得分:0)

在您的 AndroidManifest.XML 中,我认为您声明了错过匹配的名称..(class getGPS extends MapActivity,但您声明了android:name=".GPS"

 <activity
        android:name=".getGPS"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

修改

确保您的班级名称和您在android:name中声明的名称相同。

例如,如果您的课程为getGPS,则应在清单android:name=".getGPS"中声明,如上例所示。

答案 2 :(得分:0)

解决了! 这是源代码,它帮助了我:

http://code.google.com/p/androidannotations/issues/detail?id=73

原因是我在setContentView()之前编写了super.OnCreate() 它应该是这样的:

super.onCreate(savedInstanceState);
setContentView(R.layout.getgps);