我必须在Android EMulator中显示谷歌地图,但它崩溃,甚至每当我下载谷歌地图的代码,我可以api键,所有其他库但应用程序崩溃,这是我当前的代码。
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
LatLng latLng = new LatLng(13.05241, 80.25082);
map.addMarker(new MarkerOptions().position(latLng).title("Raj Amal"));
}}
但是当map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
及以下是我的日志猫
03-21 07:24:33.904: E/AndroidRuntime(1039): FATAL EXCEPTION: main
03-21 07:24:33.904: E/AndroidRuntime(1039): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amal.googlemap/com.amal.googlemap.MainActivity}: java.lang.NullPointerException
03-21 07:24:33.904: E/AndroidRuntime(1039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-21 07:24:33.904: E/AndroidRuntime(1039): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-21 07:24:33.904: E/AndroidRuntime(1039): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-21 07:24:33.904: E/AndroidRuntime(1039): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-21 07:24:33.904: E/AndroidRuntime(1039): at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 07:24:33.904: E/AndroidRuntime(1039): at android.os.Looper.loop(Looper.java:137)
03-21 07:24:33.904: E/AndroidRuntime(1039): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-21 07:24:33.904: E/AndroidRuntime(1039): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 07:24:33.904: E/AndroidRuntime(1039): at java.lang.reflect.Method.invoke(Method.java:525)
03-21 07:24:33.904: E/AndroidRuntime(1039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-21 07:24:33.904: E/AndroidRuntime(1039): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-21 07:24:33.904: E/AndroidRuntime(1039): at dalvik.system.NativeStart.main(Native Method)
03-21 07:24:33.904: E/AndroidRuntime(1039): Caused by: java.lang.NullPointerException
03-21 07:24:33.904: E/AndroidRuntime(1039): at com.amal.googlemap.MainActivity.onCreate(MainActivity.java:18)
03-21 07:24:33.904: E/AndroidRuntime(1039): at android.app.Activity.performCreate(Activity.java:5133)
03-21 07:24:33.904: E/AndroidRuntime(1039): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-21 07:24:33.904: E/AndroidRuntime(1039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-21 07:24:33.904: E/AndroidRuntime(1039): ... 11 more
答案 0 :(得分:1)
你应该像这样实施
int status = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(youractivity.this);
if (status == ConnectionResult.SUCCESS) {
setUpMapIfNeeded();
} else {
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, youractivity.this,
requestCode);
dialog.show();
}
对于设置 Google Play服务库,请转到official docs
答案 1 :(得分:1)
可以在模拟器中显示谷歌地图,你必须创建没有谷歌API的avd并安装Google_Play_services_4.0.31.apk和com.android.vending.apk(google play store)然后运行你的应用程序
让我知道是否有任何问题,如果工作正常,请选择我的答案