我是android编程的新手。我尝试在Android设备上显示简单的谷歌地图(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();
}
}
log cat
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);”时它没有找到Source!
答案 0 :(得分: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,看看这是否对您有所帮助: