Java 如何从父活动类获取数据格度和经度

Java 如何从父活动类获取数据格度和经度,java,android,google-maps,latitude-longitude,Java,Android,Google Maps,Latitude Longitude,我有一个类助手函数,取GPS的位置,我扩展到类活动。然后我有一个类加载器来显示一个初始屏幕,并获取类加载器类helper的latitude和longitude位置,我扩展到helper类。 但是为什么第一次运行always nullpointerexception时会出现这种情况呢 这是类装入器和我的助手类: public class LocationHelper extends Activity { private static LocationHelper mInstance = new L

我有一个类助手函数,取GPS的位置,我扩展到类活动。然后我有一个类加载器来显示一个初始屏幕,并获取类加载器类helper的latitude和longitude位置,我扩展到helper类。 但是为什么第一次运行always 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);
}}
这是LocationHelper:

 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();
        }

    }

}
公共类LocationAdapter扩展LocationHelper{
私有LocationHelper=LocationHelper.getInstance();
私人位置;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);getWindow().requestFeature(Window.FEATURE\u NO\u TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.splash\u screen\u loader);
splash_text=(TextView)findViewById(R.id.text_splash);
新建LoadDataTour().execute();
公共类LoadDataTour扩展
异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
试试{//我从这里得到了错误
if(helper.getLocation()!=null){
userLocation=helper.getLocation();
}else if(helper.getLocationProvider()!=null){
userLocation=new LatLng(helper.getLocationProvider()
.getLatitude(),helper.getLocationProvider()
.getLongitude());
}否则{
Toast.makeText(getApplicationContext(),
“获取位置时出错”,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());
}否则{
Toast.makeText(getApplicationContext(),
“获取位置时出错”,Toast.LENGTH_LONG.show();
}
}捕获(初始化错误例外){
Toast.makeText(getApplicationContext(),
获取数据位置时出错:“+ex.toString(),
Toast.LENGTH_LONG).show();
例如printStackTrace();
}
}
}
在哪里

@Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        return null;
    }
异步任务中的方法?我认为问题是生成了空指针异常…请检查它。
还有一件事要说明的是,使用in-onCreate方法声明异步任务时,它应该位于任何方法的外侧。

请指定应用程序执行此代码时,哪一行有NPE9:userLocation=new LatLng(helper.getLocationProvider().getLatitude(),helper.getLocationProvider().getLongitude())请把日志寄出去