Google Maps v2在模拟器上获取当前位置

时间:2014-04-16 18:48:11

标签: java android

首先,在我更改代码以获取当前位置之前,谷歌地图在模拟器上工作正常,因此我的API密钥和谷歌库以及其他所有地图都能正确设置,现在尝试获取当前位置即使在发送之后GPS协调我的应用程序崩溃与空指针异常(我是java的新手,错误记录系统吸引大量时间,每个错误都是空的异常,就像现在真的一样?? ..可以返回可以'找到最后的已知位置或东西而不是空指针?)

爪哇:

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_NORMAL);

    double latitude = myLocation.getLatitude();

    double longitude = myLocation.getLongitude();

    LatLng latLng = new LatLng(latitude, longitude);

    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
    googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));

Mainfest:

    <permission
    android:name="com.twostarii.googlemap.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="com.twostarii.googlemap.permission.MAPS_RECEIVE"/>    

<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"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>

    <meta-data 
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="MY KEY"/>

的活动:

    <fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

日志:

04-16 14:45:37.733: E/AndroidRuntime(1238): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twostarii.googlemap/com.twostarii.googlemap.MainActivity}: java.lang.NullPointerException
04-16 14:45:37.733: E/AndroidRuntime(1238):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at android.os.Looper.loop(Looper.java:136)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at java.lang.reflect.Method.invokeNative(Native Method)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at java.lang.reflect.Method.invoke(Method.java:515)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at dalvik.system.NativeStart.main(Native Method)
04-16 14:45:37.733: E/AndroidRuntime(1238): Caused by: java.lang.NullPointerException
04-16 14:45:37.733: E/AndroidRuntime(1238):     at com.twostarii.googlemap.MainActivity.setupMapIfNeeded(MainActivity.java:28)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at com.twostarii.googlemap.MainActivity.onCreate(MainActivity.java:22)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at android.app.Activity.performCreate(Activity.java:5231)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-16 14:45:37.733: E/AndroidRuntime(1238):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-16 14:45:37.733: E/AndroidRuntime(1238):     ... 11 more

1 个答案:

答案 0 :(得分:0)

Activity的{​​{1}}完成之前,{p> onCreate()的{​​{1}}被调用。您需要等到Fragment完成后才能暂停onCreateView()及其onCreateView()。例如,Fragment中的GoogleMaponResume()是抓住onStart()的好地方。

此外,Activity在此处始终为空:

Fragment

此处永远不会为空(一旦将代码移至googleMapprivate void setupMapIfNeeded() { // TODO Auto-generated method stub if (googleMap == null){ }

onStart()

因此不需要进行所有额外检查。