Issue
MainActivity execute external asynctask class
Here my code
public class MainActivity extends AppCompatActivity implements View.OnClickListener
...
public void onFirstBtnClick()
{
AysncClass ac = new AyncClass();
ac.execute();
}
and external asynctask
public class AysncClass extends AsyncTask<String, String, Integer>
@Override
protected Integer doInBackground(String... strings) {
Method1(strings[0], Integer.parseInt(strings[1]));
return null;
}
public Method1(Strins s, int i)
{
onProgressUpdate("first start");
publishProgress();
// do more work
onProgressUpdate("second start");
publishProgress();
}
public void Method2()
{
onProgressUpdate("Method2 here");
publishProgress();
}
...
@Override
protected void onProgressUpdate(final String... values) {
super.onProgressUpdate(values);
// what can i do here?
}
}
I use runOnUiThread like this in onProgressUpdate
((MainActivity)context).runOnUiThread(new Runnable()
{
@Override
public void run()
{
((MainActivity)context).tvRead.append(values[0]);
}
});*
but It occur 'java.lang.ArrayIndexOutOfBoundsException: length=0; index=0' even though values[0] is not null.
Also I use interface
this.context.WriteText(values[0]);
It occur same error
And I do this...
((MainActivity)context).tvRead.append(values[0]);
It occur 'java.lang.RuntimeException: An error occured while executing doInBackground()' and 'CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.'
...How can I resolve?
Solution
Do not call onProgressUpdate()
. Instead just call publishProgress("The String")
.
By calling publishProgress
without a parameter you will have no values in onProgressUpdate
. That's why you get an IndexOutOfBondException.
Answered By - Murat Karagöz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.