1

I have this project:

Im trying to undestand where is the problem and how can be solved, but a this simply point I really dont know where is the problem. I have a button and a TextView. When the button is clicked this procedure is called:

android:onClick="pulsaboton"

And the TextView show me the output.

This is Main_Activity.java

package com.example.pruebasonidos;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
public String cadena1="", cadena2="";
public TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.textview01);
}
public class generamusica extends AsyncTask<String, Integer, Integer>
{
    @Override
    protected void onPreExecute(){
        tv.setText(tv.getText().toString()+"Pre Execute");  
    }

    @Override
    protected Integer doInBackground(String...strings) {

        String cadena=strings[0];
        tv.setText(tv.getText().toString()+cadena); 
        return null;
    }
    @Override
    protected void onPostExecute(Integer bytes){
        tv.setText(tv.getText().toString()+"Post Execute"); 
    }

}
public void pulsaboton(View v) {
cadena1="123"; cadena2="111";
tv.setText("");
new generamusica().execute(cadena1);
new generamusica().execute(cadena2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

When pulsaboton is clicked, textview1 display this:

PreExecutePreExecute123111PostExecutePostExecute

I want this output:

PreExecute123PostExecutePreExecute111PostExecute

What is the error????

josemfr
  • 15
  • 2

3 Answers3

0

WARNING: INTERFACE MODIFICATION EXECUTED OUTSIDE OF UI THREAD!

protected Integer doInBackground(String...strings) {
    ...
    tv.setText(tv.getText().toString()+cadena); 
    ...
}

Why do you think AsyncTask exposes onPreExecute and onPostExecute? Couldn't you just do all of your interface work in doInBackground, before and after your async code?

In Android, the UI needs to be accessed from the main thread, the foreground thread; which doInBackground is not run in.

If you need to post updates to your UI during the execution of an AsyncTask, use the publishProgress mechanism.

Edit: "properly" accesing the interface is a lot more complex that just using onPreExecute and onPostExecute. See this blog post. Maybe you should try the Loader API, it's less troublesome.

salezica
  • 74,081
  • 25
  • 105
  • 166
0

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution..

http://developer.android.com/reference/android/os/AsyncTask.html

You are updating ui on the background thread which is not possible. You need to update ui on the ui thread

In doInBackground()

   runOnUiThread(new Runnable() //run on ui thread
             {
              public void run() 
              {       
                   String cadena=strings[0];
                   tv.setText(tv.getText().toString()+cadena); ;
             }
             });
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

For your required output Call these two lines in onPostExecute,

 cadena2="111";
    new generamusica().execute(cadena2);

And dont try to update the UI in background thread.

See This for more info

Community
  • 1
  • 1
Abhi
  • 8,935
  • 7
  • 37
  • 60