3

我是安卓编程新手。我尝试在安卓设备(4.1.2)上显示简单的谷歌地图。

我在 ActivityManifest.xml 上添加了所有权限和 google api 密钥

这是activity_maps.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment 
     android:id="@+id/map"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     class="com.google.android.gms.maps.MapFragment"/>

</RelativeLayout>

这是 MapsActivity.java

package com.example.androidmapsapp;

import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.maps.MapActivity;

public class MapsActivity extends MapActivity {

GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    try {
        // Loading map
        initilizeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }

    //GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
    //googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.maps, menu);
    return true;
}

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

private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}

}

原木猫

09-26 17:09:51.476: E/Trace(26750): error opening trace file: No such file or directory (2)
09-26 17:09:51.501: I/System.out(26750): Sending WAIT chunk
09-26 17:09:51.501: W/ActivityThread(26750): Application com.example.androidmapsapp is waiting for the debugger on port 8100...
09-26 17:09:51.526: I/dalvikvm(26750): Debugger is active
09-26 17:09:51.716: I/System.out(26750): Debugger has connected
09-26 17:09:51.716: I/System.out(26750): waiting for debugger to settle...
09-26 17:09:51.916: I/System.out(26750): waiting for debugger to settle...
09-26 17:09:52.116: I/System.out(26750): waiting for debugger to settle...
09-26 17:09:52.316: I/System.out(26750): waiting for debugger to settle...
09-26 17:09:52.516: I/System.out(26750): waiting for debugger to settle...
09-26 17:09:52.716: I/System.out(26750): waiting for debugger to settle...
09-26 17:09:52.921: I/System.out(26750): waiting for debugger to settle...
09-26 17:09:53.121: I/System.out(26750): debugger has settled (1377)
09-26 17:09:54.416: D/dalvikvm(26750): threadid=1: still suspended after undo (sc=1 dc=1)
09-26 17:11:15.291: E/Trace(27618): error opening trace file: No such file or directory (2)
09-26 17:11:15.306: I/System.out(27618): Sending WAIT chunk
09-26 17:11:15.306: W/ActivityThread(27618): Application com.example.androidmapsapp is waiting for the debugger on port 8100...
09-26 17:11:15.316: I/dalvikvm(27618): Debugger is active
09-26 17:11:15.506: I/System.out(27618): Debugger has connected
09-26 17:11:15.506: I/System.out(27618): waiting for debugger to settle...
09-26 17:11:15.706: I/System.out(27618): waiting for debugger to settle...
09-26 17:11:15.906: I/System.out(27618): waiting for debugger to settle...
09-26 17:11:16.106: I/System.out(27618): waiting for debugger to settle...
09-26 17:11:16.306: I/System.out(27618): waiting for debugger to settle...
09-26 17:11:16.511: I/System.out(27618): waiting for debugger to settle...
09-26 17:11:16.711: I/System.out(27618): waiting for debugger to settle...
09-26 17:11:16.911: I/System.out(27618): debugger has settled (1404)
09-26 17:11:18.246: D/dalvikvm(27618): threadid=1: still suspended after undo (sc=1 dc=1)
09-26 17:11:23.271: D/dalvikvm(27618): threadid=1: still suspended after undo (sc=1 dc=1)
09-26 17:11:23.271: D/dalvikvm(27618): GC_CONCURRENT freed 172K, 9% free 9639K/10503K, paused 22ms+4ms, total 56ms
09-26 17:11:23.276: W/CursorWrapperInner(27618): Cursor finalized without prior close()

在调试“setContentView(R.layout.activity_maps);”时 它给出了找不到源!

4

1 回答 1

1

我认为您的问题是您的活动扩展了这一事实,MapActivity这对于 Google Map API V1 是正确的,但不应该用于 Google Maps API V2。应该使用的是简单Activity的还是FragmentActivity取决于您所针对的平台。

在您的情况下,只需尝试更改此行:

public class MapsActivity extends MapActivity {

对此:

public class MapsActivity extends Activity {

如果您仍然遇到问题,您可以阅读我写的这篇博文,将 Google Maps API V2 集成到您的应用程序中,看看这是否对您有帮助:

谷歌地图 API V2

于 2013-09-27T06:32:52.970 回答