我有一个类助手功能,取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);
}}
这是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();
}
}
}
答案 0 :(得分:0)
在哪里
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
你的异步任务中的方法?我认为问题是因此生成空指针异常...检查它。 还有一件事你在onCreate方法中声明异步任务它应该在任何方法之外。