获取当前位置值(lat,lng)的NullPointerException Google Maps

时间:2015-08-29 04:30:13

标签: java android google-maps nullpointerexception

当我使用地图片段时,获取当前位置的代码正常工作,但是当我在另一个类中使用它而不显示地图时,我在String提供程序中得到nullpointerException。你能否检查我的程序有什么问题?如果可能的话,请解释我为什么会收到这个错误。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View v = inflater
            .inflate(R.layout.layout_school_list, container, false);
    lvList = (ListView) v.findViewById(R.id.lvSchools);
    LocationManager locationManager = (LocationManager) getSystemService("location");
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    final Location currentLoc = locationManager
            .getLastKnownLocation(provider);
    if (currentLoc != null) {
        onLocationChanged(currentLoc);
    }

    if (Distance==true){
        double lat = currentLoc.getLatitude();
        double lng = currentLoc.getLongitude();
        ArrayList<Double> rangeList = getRangeList(lat, lng);
        ArrayList<String> theList1 = new ArrayList<String>();
        for (int count = 0; count < rangeList.size(); count++)
        {
            String temp = sTuition[count];
            theTuitionFee = Integer.parseInt(temp);
            for(int subloop=0; subloop<sSpecialty[count].length; subloop++) {
            List<String> values = (List<String>)thisFilter[4];
            if (rangeList.get(count) <= DistanceVal
                    &&sReg[count].equalsIgnoreCase((String)thisFilter[0])
                    && sAdmin[count].equalsIgnoreCase((String)thisFilter[1])
                    && sAmbience[count].equalsIgnoreCase((String)thisFilter[3])
                    && amountTF >= theTuitionFee
                    && filtered(sSpecialty[count][subloop], values)) {
                theList1.add(sList[count]);

            }
            }
        }

        displayNearbyList(theList1);
    }

这是我的方法:

public void onLocationChanged(Location location) {
    double currentLat = location.getLatitude();
    double currentLng = location.getLongitude();

}

并且getSystemService和我的IDE也遇到错误需要我把这个

 private LocationManager getSystemService(String string) {
    // TODO Auto-generated method stub
    return null;
}

在我的其他工人阶级没有使用。

这是我的堆栈跟踪。我是新来的,所以我不知道如何正确发布堆栈跟踪

08-29 12:14:45.017:E / AndroidRuntime(6864):致命异常:主要 08-29 12:14:45.017:E / AndroidRuntime(6864):java.lang.NullPointerException 08-29 12:14:45.017:E / AndroidRuntime(6864):at capstone.app.recommender.SchoolListFragmentFilter.onCreateView(SchoolListFragmentFilter.java:67) 08-29 12:14:45.017:E / AndroidRuntime(6864):在android.support.v4.app.Fragment.performCreateView(Fragment.java:1786) 08-29 12:14:45.017:E / AndroidRuntime(6864):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947) 08-29 12:14:45.017:E / AndroidRuntime(6864):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126) 08-29 12:14:45.017:E / AndroidRuntime(6864):在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739) 08-29 12:14:45.017:E / AndroidRuntime(6864):在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489) 08-29 12:14:45.017:E / AndroidRuntime(6864):在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:454) 08-29 12:14:45.017:E / AndroidRuntime(6864):在android.os.Handler.handleCallback(Handler.java:725) 08-29 12:14:45.017:E / AndroidRuntime(6864):在android.os.Handler.dispatchMessage(Handler.java:92) 08-29 12:14:45.017:E / AndroidRuntime(6864):在android.os.Looper.loop(Looper.java:137) 08-29 12:14:45.017:E / AndroidRuntime(6864):在android.app.ActivityThread.main(ActivityThread.java:5041) 08-29 12:14:45.017:E / AndroidRuntime(6864):at java.lang.reflect.Method.invokeNative(Native Method) 08-29 12:14:45.017:E / AndroidRuntime(6864):at java.lang.reflect.Method.invoke(Method.java:511) 08-29 12:14:45.017:E / AndroidRuntime(6864):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 08-29 12:14:45.017:E / AndroidRuntime(6864):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08-29 12:14:45.017:E / AndroidRuntime(6864):at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

你应该替换你的行

LocationManager locationManager = (LocationManager) getSystemService("location");

有了这个

LocationManager locationManager = (LocationManager)<your context>.getSystemService(Context.LOCATION_SERVICE);

答案 1 :(得分:0)

LoadModule rewrite_module modules/mod_rewrite.so

当你返回 null 时,这是你必须自己实现的方法。下面的 private LocationManager getSystemService(String string) { // TODO Auto-generated method stub return null; } null ,因此会抛出NullPointerException

locationManager

查看下面的帖子,了解如何实施String provider = locationManager.getBestProvider(criteria, true);

How does getSystemService() work exactly?