NullPointerException即将发布在谷歌地图加载方法上

时间:2014-12-15 08:33:17

标签: android

public void drawMarker(ArrayList<Datamodel> arrModel) {
    if(map!=null)
    {

    String lat = "";
    String lng = "";

    SharedPreferences prefs = getActivity().getSharedPreferences("Value",
            Context.MODE_PRIVATE);

    locationEnabledisablevalue = prefs.getString("GpsEnableValue", "1");

    if (!locationEnabledisablevalue.equals("1")) {
        map.clear();
        return;
    }

    for (int i = 0; i < arrModel.size(); i++) {
        // Drawing marker on the map
        Datamodel datamodel = arrModel.get(i);

        // Getting the latitude of the i-th location
        lat = datamodel.lat;

        // Getting the longitude of the i-th location
        lng = datamodel.lon;

        LatLng point = null;
        if (lat != "" && lng != "") {
            point = new LatLng(Double.parseDouble(lat),
                    Double.parseDouble(lng));
        }
        // Creating an instance of MarkerOptions
        MarkerOptions markerOptions = new MarkerOptions();

        // Setting latitude and longitude for the marker
        markerOptions.position(point);
        markerOptions.title(datamodel.contactName);

        if (datamodel.id
                .equals(((HomeActivity) getActivity()).NotifcationRecciverid)) {

            String ContactLat = "";
            String ContactLng = "";

            String Notificationlat = ((HomeActivity) getActivity()).notifcationlat;
            String Notificationlng = ((HomeActivity) getActivity()).notifcationlng;
            String Notifcationreccvierid = ((HomeActivity) getActivity()).NotifcationRecciverid;

            try {
                DataBaseManager db = new DataBaseManager(getActivity());
                Cursor cursor = db.fetchContactInfo(Notifcationreccvierid);

                if (cursor != null && cursor.moveToFirst()) {
                    ContactLat = cursor
                            .getString(cursor
                                    .getColumnIndex(DataProviderConstants.CONTACT_LAT_COLUMN));

                    ContactLng = cursor
                            .getString(cursor
                                    .getColumnIndex(DataProviderConstants.CONTACT_LON_COLUMN));

                    cursor.close();
                }
            } catch (NullPointerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            float distance = AdapterHome.findDistance(
                    Double.parseDouble(Notificationlat),
                    Double.parseDouble(Notificationlng),
                    Double.parseDouble(ContactLat),
                    Double.parseDouble(ContactLng));
            String time = ((HomeActivity) getActivity()).NotifcationDatetime;
            markerOptions
                    .snippet(((HomeActivity) getActivity()).nOtifcationMessge
                            + "  " + Functions.curentdatetimeprint(time));

            marker = map.addMarker(markerOptions);
            LatLng myPoint = new LatLng(Double.parseDouble(ContactLat),
                    Double.parseDouble(ContactLng));
            // // Move the camera instantly to hamburg with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(myPoint, 15));
            //
            // // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
            // if (distance < 250) {
            marker.showInfoWindow();
            // }
            // }

        } else {
            markerOptions.snippet(datamodel.phoneNumber);
            map.addMarker(markerOptions);

        }
    }
    }

这是Google地图上的代码显示标记。 in public void onResume()方法我调用这个drawMarker(arrModel);但有些时候应用程序崩溃并在行

显示错误Logcat问题
 .SharedPreferences prefs = getActivity().getSharedPreferences("Value",
                Context.MODE_PRIVATE);
        locationEnabledisablevalue = prefs.getString("GpsEnableValue", "1");


logcat的: -

   12-15 10:29:02.494: E/AndroidRuntime(16303): FATAL EXCEPTION: main
    12-15 10:29:02.494: E/AndroidRuntime(16303): java.lang.NullPointerException
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at com.locii.fragment.FragHome.drawMarker(FragHome.java:347)
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at com.lociiapp.HomeActivity$UpdateUserLocation.onPostExecute(HomeActivity.java:530)
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at com.lociiapp.HomeActivity$UpdateUserLocation.onPostExecute(HomeActivity.java:1)
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at android.os.AsyncTask.finish(AsyncTask.java:631)
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at android.os.Handler.dispatchMessage(Handler.java:99)
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at android.os.Looper.loop(Looper.java:137)
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at android.app.ActivityThread.main(ActivityThread.java:5136)
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at java.lang.reflect.Method.invokeNative(Native Method)
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at java.lang.reflect.Method.invoke(Method.java:525)
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    12-15 10:29:02.494: E/AndroidRuntime(16303):    at dalvik.system.NativeStart.main(Native Method)

请告诉我如何解决此问题我无法设置空指针异常。

1 个答案:

答案 0 :(得分:0)

这可能意味着您的方法调用

getActivity().getSharedPreferences("Value", Context.MODE_PRIVATE); 

返回null,即prefsnull。在下一个语句中,您调用的prefs.getString会引发NullPointerException

找出它返回null的原因。我猜测您的偏好文件的名称(如果它存在的话)不是Value

有关如何使用此方法的信息,请参阅此链接:http://developer.android.com/reference/android/content/Context.html#getSharedPreferences%28java.lang.String,%20int%29