Android GoogleMaps 空指针异常 lagitutde

Posted

技术标签:

【中文标题】Android GoogleMaps 空指针异常 lagitutde【英文标题】:Android GoogleMaps Null Pointer Exception lagitutde 【发布时间】:2014-05-18 11:37:57 【问题描述】:

我在启动谷歌地图时遇到空指针异常。不确定问题可能是什么。在我尝试查找当前位置之前,地图正在显示。现在它只是退出了。

package com.example.postplay;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
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;

public class MapActive extends FragmentActivity 

    private GoogleMap googleMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_active);

        setUpMapIfNeeded();         
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.map_active, menu);
        return true;
    

    private void setUpMapIfNeeded() 

      if(googleMap == null)
      
          googleMap = ((SupportMapFragment) getSupportFragmentManager().
                  findFragmentById(R.id.map)).getMap();
      
      if(googleMap != null)
      
          setUpMap();
      

    

    private void setUpMap() 
        // enable location layer of gmaps
        googleMap.setMyLocationEnabled(true);

        // get locationmanager object from system service
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // object to retrieve provider
        Criteria criteria = new Criteria();

        // get name of best provider
        String provider = locationManager.getBestProvider(criteria,true);

        // get current location
        Location myLocation = locationManager.getLastKnownLocation(provider);

        //set map type
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

        //get latitude of the current loc
        double latitude = myLocation.getLatitude();

        //get longitude
        double longitude = myLocation.getLongitude();

        // create a current location object
        LatLng latLng = new LatLng(latitude,longitude);

        //show current location on map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        //zoom in
        googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
        googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).
                                                             title("You are here!"));

    


这是日志猫

04-06 01:49:16.819: W/ActivityThread(28484): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
04-06 01:49:16.829: D/dalvikvm(28484): Rejecting registerization due to ushr-int/lit8 v2, v4, (#1)
04-06 01:49:16.839: D/dalvikvm(28484): Rejecting registerization due to +iget-object-quick v0, v3, (#8)
04-06 01:49:16.849: D/dalvikvm(28484): Rejecting registerization due to +iget-object-quick v4, v6, (#8)
04-06 01:49:16.849: D/dalvikvm(28484): Rejecting registerization due to +iget-object-quick v4, v6, (#8)
04-06 01:49:16.849: D/AndroidRuntime(28484): Shutting down VM
04-06 01:49:16.849: W/dalvikvm(28484): threadid=1: thread exiting with uncaught exception (group=0x41ab8e10)
04-06 01:49:16.849: E/AndroidRuntime(28484): FATAL EXCEPTION: main
04-06 01:49:16.849: E/AndroidRuntime(28484): java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.postplay/com.example.postplay.MapActive: java.lang.NullPointerException
04-06 01:49:16.849: E/AndroidRuntime(28484):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
04-06 01:49:16.849: E/AndroidRuntime(28484):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
04-06 01:49:16.849: E/AndroidRuntime(28484):    at android.app.ActivityThread.access$700(ActivityThread.java:150)
04-06 01:49:16.849: E/AndroidRuntime(28484):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
04-06 01:49:16.849: E/AndroidRuntime(28484):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 01:49:16.849: E/AndroidRuntime(28484):    at android.os.Looper.loop(Looper.java:176)
04-06 01:49:16.849: E/AndroidRuntime(28484):    at android.app.ActivityThread.main(ActivityThread.java:5279)
04-06 01:49:16.849: E/AndroidRuntime(28484):    at java.lang.reflect.Method.invokeNative(Native Method)    
04-06 01:49:16.849: E/AndroidRuntime(28484):    at java.lang.reflect.Method.invoke(Method.java:511)    
04-06 01:49:16.849: E/AndroidRuntime(28484):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)    
04-06 01:49:16.849: E/AndroidRuntime(28484):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)    
04-06 01:49:16.849: E/AndroidRuntime(28484):    at dalvik.system.NativeStart.main(Native Method)    
04-06 01:49:16.849: E/AndroidRuntime(28484): Caused by: java.lang.NullPointerException    
04-06 01:49:16.849: E/AndroidRuntime(28484):    at com.example.postplay.MapActive.setUpMap(MapActive.java:70)    
04-06 01:49:16.849: E/AndroidRuntime(28484):    at     
com.example.postplay.MapActive.setUpMapIfNeeded(MapActive.java:45)
04-06 01:49:16.849: E/AndroidRuntime(28484):    at     
com.example.postplay.MapActive.onCreate(MapActive.java:26)
04-06 01:49:16.849: E/AndroidRuntime(28484):    at     
android.app.Activity.performCreate(Activity.java:5267)
04-06 01:49:16.849: E/AndroidRuntime(28484):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
04-06 01:49:16.849: E/AndroidRuntime(28484):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
04-06 01:49:16.849: E/AndroidRuntime(28484):    ... 11 more

【问题讨论】:

第 70 行是什么MapActive.java 它的双纬度 = myLocation.getLatitude(); 【参考方案1】:
public Location getLastKnownLocation (String provider)

Added in API level 1
Returns a Location indicating the data from the last known location fix obtained from the given provider.

This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.

If the provider is currently disabled, null is returned.

Parameters
provider    the name of the provider
Returns
**the last known location for the provider, or null**

你说第 70 行是

double latitude = myLocation.getLatitude();

所以myLocation 为空

Location myLocation = locationManager.getLastKnownLocation(provider);

【讨论】:

我知道它返回 null 我只是不知道为什么 @user3404290 可能是 如果提供程序当前被禁用,则返回 null。 你是在模拟器还是真实设备上测试它 嗯如何启用提供程序 @user3404290 阅读此developer.android.com/guide/topics/location/index.html 和developer.android.com/guide/topics/location/strategies.html

以上是关于Android GoogleMaps 空指针异常 lagitutde的主要内容,如果未能解决你的问题,请参考以下文章

Android 防止空指针异常

Android中的导航抽屉空指针异常

如何解决空指针异常和 ThreadException 中的错误android

Android-空指针异常反复崩溃

Android soundpool 空指针异常

Android - 调用自定义 View 方法时出现空指针异常