【发布时间】:2018-12-19 00:32:46
【问题描述】:
[抱歉英语不好]
大家好, 在这里,我尝试在异步任务中执行某些操作,并且应用程序在运行时崩溃。当它到达返回线时,应用程序就会死掉。
这是我的代码
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;
}
}
知道这个异步任务在片段内可能很有用。
Android Studio 在错误日志中给了我这个:
原因:java.lang.RuntimeException: Can't create handler inside 没有调用 Looper.prepare() 的线程
我尝试使用没有说明的onPostExecute 方法,但没有解决问题。
【问题讨论】: