所以我严格按照这个指南来做(或者至少试着这么做)。
除了需要导入一些指南中没有指定的东西之外,其他都很好,除了有一个我无法解释的红色弯弯曲曲。
这一行:
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
在"R.id.map"中的"map"下面有一个红色标记。
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.*;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().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.main, menu);
return true;
}
}
我已经包括android-support-v4.jar我的构建路径,我已经下载了一切正确从谷歌SDK(包括谷歌播放服务)。我也有一个API密钥。
在我看来,第7步是你缺少资源的地方:
更新res/layout/activity_main.xml并替换整个内容
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map" android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
在Eclipse中,您需要:
- 修改文件
- 保存
- 清理项目(以重新生成资源,其中包括R.java)
如果您在activity_main.xml中有android:id="@+id/activity_main"
而不是android:id="@+id/map"
。
我得到了类似的错误。我取代了
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />