i´ve read a lot of post that had almost the same question but it doesn´t help me with my problem, the thing is that I am trying to create an app to constantly update the information in 2 EditTexts for Latitude and Longitude using the GPS of the device. In tutorials I had seen they create the classes inside the MainActivity so I try to do almost the same but creating a new class file. When I send the information to the MainActivity it reach the EditText but it seems that those cannot be filled with the information I don't know why... Thinking that It could be the threads I try to implement a AsynkTask (but my android skills are not to high) and i am having the same error... If some one could help me I whould be really Greatfull! I post the code next...
This is the MainActivity
public class MainActivity extends Activity {
public EditText edTLatitud;
public EditText edTLongitud;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edTLatitud = (EditText)this.findViewById(R.id.edTxtLatitud);
edTLongitud = (EditText)this.findViewById(R.id.edTxtLongitud);
ConfigGps();
}
private void ConfigGps()
{
LocationManager mLocationManager;
LocationListener mLocationListener;
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationListener = new MyLocationListener();
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000, 1, mLocationListener);
}
This is the new file class
public class MyLocationListener implements LocationListener{
private MainActivity mainActivity = new MainActivity();
private GPSBackground gpsBackground = new GPSBackground();
private String Latitud,Longitud;
@Override
public void onLocationChanged(Location location) {
gpsBackground.execute(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
private class GPSBackground extends AsyncTask<Location, Void,Void>
{
@Override
protected Void doInBackground(Location... params) {
Location location = params[0];
try{
mainActivity.edTLatitud.setText(String.valueOf(location.getLatitude()));
mainActivity.edTLongitud.setText(String.valueOf(location.getLongitude()));
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
}
The error I catch is this one:
563-572/com.apps3d.logixuexapp W/System.err: java.lang.NullPointerException 08-22 18:57:40.745 563-572/com.apps3d.logixuexapp W/System.err: at com.apps3d.logixuexapp.MyLocationListener$GPSBackground.doInBackground(MyLocationListener.java:50) 08-22 18:57:40.745 563-572/com.apps3d.logixuexapp W/System.err: at com.apps3d.logixuexapp.MyLocationListener$GPSBackground.doInBackground(MyLocationListener.java:42) 08-22 18:57:40.755 563-572/com.apps3d.logixuexapp W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:185) 08-22 18:57:40.755 563-572/com.apps3d.logixuexapp W/System.err: at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 08-22 18:57:40.755 563-572/com.apps3d.logixuexapp W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:138) 08-22 18:57:40.755 563-572/com.apps3d.logixuexapp W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 08-22 18:57:40.755 563-572/com.apps3d.logixuexapp W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 08-22 18:57:40.755 563-572/com.apps3d.logixuexapp W/System.err: at java.lang.Thread.run(Thread.java:1019)
Well if i am doing something wrong please give me a hand, I am new on this of android development jejej Thanks !!