【问题标题】:How to get data lattitude and longitude from parent activity class如何从父活动类中获取数据经纬度
【发布时间】:2014-07-22 04:48:09
【问题描述】:

我有一个类帮助函数获取 GPS 的位置,我将其扩展到类活动。然后我有一个类加载器来显示一个启动屏幕并获取类加载器类助手的纬度和经度的位置,我扩展到助手类。 但是为什么第一次运行时总是 nullpointerexception 呢?

这是类加载器和我的助手类:

public class LocationHelper extends Activity {
private static LocationHelper mInstance = new LocationHelper();
private LatLng curlatlng;
private GoogleMap mMap;
LocationManager locManager;
LocationListener locListener;
Location location;

public LocationHelper() {
    super();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    initLocationManager();
}

public static LocationHelper getInstance() {
    return mInstance;
}

public LatLng getLocation() {
    return curlatlng;
}

public Location getLocationProvider() {
    return location;
}

private void initLocationManager() {
    new Handler().postDelayed(new Thread() {
        @Override
        public void run() {
            try {
                curlatlng = new LatLng(mMap.getMyLocation().getLatitude(),
                        mMap.getMyLocation().getLongitude());
            } catch (Exception e) {
                Log.d("error", e.toString());
            }
        }
    }, 3000);

    locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locListener = new LocationListener() {

        public void onLocationChanged(Location newLocation) {

        }

        public void onProviderDisabled(String arg0) {
            Intent intent = new Intent(
                    Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }

        public void onProviderEnabled(String arg0) {
            location = locManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }

        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        }

    };
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            locListener);
    location = locManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
}}

这是定位助手:

 public class LocationAdapter extends LocationHelper {
 private LocationHelper helper = LocationHelper.getInstance();
 private LatLng userLocation;
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.splash_screen_loader);
    splash_text = (TextView) findViewById(R.id.text_splash);

    new LoadDataTour().execute();

    public class LoadDataTour extends
        AsyncTask<String, Void, ArrayList<HashMap<String, String>>> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        try { //i get error from here
            if (helper.getLocation() != null) {
                userLocation = helper.getLocation();
            } else if (helper.getLocationProvider() != null) {
                userLocation = new LatLng(helper.getLocationProvider()
                        .getLatitude(), helper.getLocationProvider()
                        .getLongitude());
            } else {
                Toast.makeText(getApplicationContext(),
                        "Error Get Location", Toast.LENGTH_LONG).show();
            }

            if (userLocation != null) {
                setLat(String.valueOf(userLocation.latitude));
                setLng(String.valueOf(userLocation.longitude));
                setLinkUrl(URL_WEB + "tabel=" + getTabel() + "&lat="
                        + getLat() + "&long=" + getLng());
            } else {
                Toast.makeText(getApplicationContext(),
                        "Error Get Location", Toast.LENGTH_LONG).show();
            }
        } catch (ExceptionInInitializerError ex) {
            Toast.makeText(getApplicationContext(),
                    "Error get data position : " + ex.toString(),
                    Toast.LENGTH_LONG).show();
            ex.printStackTrace();
        }

    }

}

【问题讨论】:

  • 请指定哪一行有NPE
  • 当应用程序执行此代码时:userLocation = new LatLng(helper.getLocationProvider() .getLatitude(), helper.getLocationProvider() .getLongitude());
  • 请张贴logcat

标签: java android google-maps latitude-longitude


【解决方案1】:

在哪里

@Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        return null;
    }

异步任务中的方法?我认为问题是因此产生了空指针异常...检查一下。 还有一件事要您在 onCreate 方法中声明异步任务,它应该在任何方法之外..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多