Issue
[Sorry for bad english]
Hello everyone, Here im trying to do something inside an async task and the app crashes when running it. When it reaches the return line the app dies.
Here is my code
private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params){
velocidadMedia = (distanciaTotal)/(tiempoTotal);
track = new TrackItem( sdf.format(new Date()),
Double.toString((int) distanciaTotal),
Double.toString((int) tiempoTotal),
Double.toString((int) velocidadMedia));
String IDFromServer = databaseReference.push().getKey();
track.setKey(IDFromServer);
databaseReference.child(IDFromServer).setValue(track);
Toast.makeText(getContext(),"Track Creado!", Toast.LENGTH_LONG).show();
return null;
}
}
It might be useful to know this async task is inside a fragment.
Android studio is giving me this in the error log:
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
I tried using the onPostExecute
method with no instructions in it but did not solve the issue.
Solution
You cannot call the Toast.makeText()
from a background thread. You can only call from the UI/Main thread. Move the Toast.makeText()
call to onPostExecute()
.
Answered By - Jayson Chacko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.