我无法让我的地图显示在Android应用上。我已经观看了视频并阅读了很多关于问题排查的论坛(包括这个论坛)。我的代码没有显示任何错误,但每次我运行它是否在手机,平板电脑或模拟器上它说“不幸的是,东西已停止”。任何帮助或帮助将不胜感激。 SN:SDK和Google Play服务是最新的。
package com.stuff;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// TODO Auto-generated method stub
if (googleMap == null) {
googleMap = ((SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (googleMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
// TODO Auto-generated method stub
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(10));
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,
longitude)).title("You are here!"));
}
@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;
}
}
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stuff"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are required for location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAQz_W-qNqgpXp8SZG3sqAt_YuVVSalCg8"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
07-25 01:54:10.266: E/Trace(1074): error opening trace file: No such file or directory (2)
07-25 01:54:11.085: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.085: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.105: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.105: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.117: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.117: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.145: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.165: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.208: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.325: D/AndroidRuntime(1074): Shutting down VM
07-25 01:54:11.325: W/dalvikvm(1074): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-25 01:54:11.355: E/AndroidRuntime(1074): FATAL EXCEPTION: main
07-25 01:54:11.355: E/AndroidRuntime(1074): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stuff/com.stuff.MainActivity}: java.lang.NullPointerException
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.os.Looper.loop(Looper.java:137)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-25 01:54:11.355: E/AndroidRuntime(1074): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:54:11.355: E/AndroidRuntime(1074): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 01:54:11.355: E/AndroidRuntime(1074): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-25 01:54:11.355: E/AndroidRuntime(1074): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-25 01:54:11.355: E/AndroidRuntime(1074): at dalvik.system.NativeStart.main(Native Method)
07-25 01:54:11.355: E/AndroidRuntime(1074): Caused by: java.lang.NullPointerException
07-25 01:54:11.355: E/AndroidRuntime(1074): at com.stuff.MainActivity.setUpMapIfNeeded(MainActivity.java:33)
07-25 01:54:11.355: E/AndroidRuntime(1074): at com.stuff.MainActivity.onCreate(MainActivity.java:26)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.Activity.performCreate(Activity.java:5104)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-25 01:54:11.355: E/AndroidRuntime(1074): ... 11 more
07-25 01:54:11.536: D/dalvikvm(1074): GC_CONCURRENT freed 140K, 9% free 2701K/2960K, paused 19ms+4ms, total 216ms
07-25 01:54:13.585: I/Process(1074): Sending signal. PID: 1074 SIG: 9
答案 0 :(得分:1)
您是否使用sdk管理器安装了Google Play服务?
答案 1 :(得分:1)
您的getMap()
返回null。
公共最终GoogleMap getMap()
获取与此包装的视图关联的基础GoogleMap 片段。
返回GoogleMap。如果片段的视图尚未显示为空 准备。如果片段生命周期没有通过,就会发生这种情况 onCreateView(LayoutInflater,ViewGroup,Bundle)呢。这也可以 如果Google Play服务不可用,则会发生。如果谷歌播放 服务随后可用,片段已经消失 通过onCreateView(LayoutInflater,ViewGroup,Bundle),调用它 方法将再次初始化并返回GoogleMap。
所以你需要等到片段准备就绪,并在初始化GooleMap对象之前检查可用性google play服务。
答案 2 :(得分:1)
尝试将您的布局更改为:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
或
if (googleMap == null) {
googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
}
答案 3 :(得分:0)
试试这段代码。
SupportMapFragment mFragment;
mFragment = new SupportMapFragment() {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if ((map = mFragment.getMap()) != null) {
setUpMap();
}
}
};