所以我完全按照this的指导来做(或者至少尝试这样做)。
除了必须导入一些指南中没有指定的东西之外,一切都很好,除了有一个我无法解释的红色曲折。
在这一行中:
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、我的构建路径,并且我已经从Google SDK正确下载了所有内容(包括Google Play服务)。我也有一个API密钥。显然,它现在应该可以工作了。
发布于 2019-02-26 18:18:28
如果在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" />https://stackoverflow.com/questions/16348982
复制相似问题