All the methods are invoked in MainActivity. In my app i'm getting an HTML page from ion library and i have some methods for format the text i've downloaded with ion.
That what i'm trying to do is to set variable DoveSono
in a TextView, actually at every cycle of the loop that variable changes so the TextView should change too but actually with what i've done the app crash.
This is the method that i invoke onClick and start ion with getting the HTML website then it invoke other methods:
private void getHTMLArticoli(){
// progressDialog = new SpotsDialog(articoli.this, R.style.Custom);
// progressDialog.show();
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(new Runnable() {
@Override
public void run() {
htmlresultart = null;
try {
htmlresultart = Ion.with(getApplicationContext())
.load("WEBSITEIP")
.asString()
.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
if (htmlresultart != null) {
htmlresultart = htmlresultart.replace("</td>", "\n");
getTableArticoli();
getTestataArticoli();
getBodyArticoli();
// progressDialog.cancel();
}
While here is the method getBodyArticoli that i calls in getHTMLArticoli here you can find the part where i'm trying to setText
private void getBodyArticoli(){
DaDoveParto = Integer.valueOf(String.valueOf(htmlresultart.indexOf("TBLCRP")));
DoveMiFermo = Integer.valueOf(String.valueOf(htmlresultart.indexOf("</form>")));
if(DaDoveParto == 0){
Toast.makeText(this,"NESSUN DATO TROVATO",Toast.LENGTH_SHORT).show();
}else
{
Integer i;
Integer j;
Integer CONTACAMPO = 0;
for( i = DaDoveParto ; i <= DoveMiFermo ; i++){
if( htmlresultart.substring(i, i + 4).equals("<td>")){
i += 4;
for (j = i; j <= DoveMiFermo ; j++){
if(htmlresultart.substring(j, j + 1).equals("\n")){
appBODYart[CONTACAMPO] = htmlresultart.substring(i, i + (j - i));
if(appBODYart[CONTACAMPO].equals("(null)")){
appBODYart[CONTACAMPO] = "";
}
CONTACAMPO += 1;
if(CONTACAMPO.equals(QuantiCampi)){
CONTACAMPO = 0;
myDB.insertArtServer(appBODYart[0], appBODYart[1], appBODYart[2], appBODYart[3], appBODYart[4], appBODYart[5],
appBODYart[6], appBODYart[7], appBODYart[8]);
DoveSono +=0;
textView.setText(String.valueOf(DoveSono);
}
break;
Actually when i'm trying to do it i'm getting the following error:
05-31 17:24:41.515 4605-4769/com.example.igardini.visualposmobile E/AndroidRuntime: FATAL EXCEPTION: pool-2-thread-1 Process: com.example.igardini.visualposmobile, PID: 4605 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6363) at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:874) at android.view.View.requestLayout(View.java:17484) at android.view.View.requestLayout(View.java:17484) at android.view.View.requestLayout(View.java:17484) at android.view.View.requestLayout(View.java:17484) at android.view.View.requestLayout(View.java:17484) at android.view.View.requestLayout(View.java:17484) at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:3112) at android.view.View.requestLayout(View.java:17484) at android.widget.TextView.checkForRelayout(TextView.java:6932) at android.widget.TextView.setText(TextView.java:4083) at android.widget.TextView.setText(TextView.java:3941) at android.widget.TextView.setText(TextView.java:3916) at com.example.igardini.visualposmobile.articoli.getBodyArticoli(articoli.java:121) at com.example.igardini.visualposmobile.articoli.access$300(articoli.java:17) at com.example.igardini.visualposmobile.articoli$2.run(articoli.java:79) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)
I just had to wrap the
textView.setText(String.valueOf(DoveSono * 100 / QuanteRighe));
with
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(String.valueOf(DoveSono * 100 / QuanteRighe));
}
});