Have read all the set and get called from the UI thread, on stackoverflow but none seems relevent. this code worked 2years ago, recently opened it. and it shows this error. "Method setText must be called from the UI thread".
What updates are doing this?
marked the setText lines with comments to make it more clear. Any sugestions?
public class MainActivity extends Activity {
static final String baseURL = "http://api.yr.no/weatherapi/locationforecast/1.9/?lat=";
TextView dato, grader, vindhastighet, vindretning;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .detectAll().penaltyLog().build(); StrictMode.setThreadPolicy(policy);
SmartLocation.with(this).location()
.oneFix()
.start(new OnLocationUpdatedListener() {
@Override
public void onLocationUpdated(Location location) {
dato = (TextView)findViewById(R.id.gpsbydato_txt);
grader = (TextView)findViewById(R.id.gpsbygrader_txt);
vindhastighet = (TextView)findViewById(R.id.gpsbyvindhastighet_txt);
vindretning = (TextView)findViewById(R.id.gpsbyvindretning_txt);
Double latitude = location.getLatitude();
Double longtitude = location.getLongitude();
StringBuilder Url = new StringBuilder(baseURL);
Url.append(latitude + ";lon=" + longtitude);
final String fullUrl = Url.toString();
class Read extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... arg0) {
try {
URL website = new URL(fullUrl);
//xmlreader parser data
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
HandlingXMLStuff doingWork = new HandlingXMLStuff();
xr.setContentHandler(doingWork);
xr.parse(new InputSource(website.openStream()));
String informationTemp = doingWork.getInformationTemp();
String informationVindretning = doingWork.getInformationVindretning();
String informationVindhastighet = doingWork.getInformationVindhastighet();
//grader.setText is causing the error.
grader.setText(informationTemp);
//vindhastighet.setText is causing the error.
vindhastighet.setText(informationVindhastighet);
//vindretning.setText is causing the error.
vindretning.setText(informationVindretning);
} catch (Exception e) {
Log.e("URL:", fullUrl);
dato.setText("erroorrr"); //also this setText
System.out.println(e);
}
return null;
}
@Override
protected void onPostExecute(String result) {
dato.setText(result);
super.onPostExecute(result);
}
}
}
});
}
}