3
I am facing a strange problem in my application, since in I am testing in 4 different devices but only two of them have the expected behavior.
Scenario / Devices:
- Nexus 4 / Android 5.0 - OK
- Galaxy Tab 2 / Android 4.2.2 - OK
- LG Optimus / Android 4.4.2 - Error
- Genesis / Android 4.1.2 - Error
As you can see, 4 versions of Android different and only the first two are working.
The problem in question is that the first time I install the application on the device, I run a task with AsyncTask
and it all happens normally. But from then on I can’t perform anymore.
As the method onPreExecute()
runs in the same thread, man debug gets into it without problem, but not in the doInBackground()
.
I read something about the method executeOnExecutor()
from API 11 (my application is from 15) but I don’t think that’s the case, since I don’t want several tasks in parallel but a simple.
The simple representation of my code is this:
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
// Preparo minha UI
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// Executo minha tarefa
return null;
}
@Override
protected void onPostExecute(Void result) {
// Conclusão
super.onPostExecute(result);
}
}.execute();
Anyway, I don’t know what might be happening, I’ve done a lot of research and found nothing. My development environment is the Android Studio 1.0.1, compiled with SDK version 21.
Only one doubt, where exactly you call the
.execute()
? Would be inside aonClick
or in theonCreate
?– Guilherme Nascimento
I have tasks in the
onCreate
of ativity andonClick
of a button too, but in all of them the same problem occurs, after working the first time after an installation, no longer works.– Paulo Rodrigues
You mean you use the same
AsyncTask
for various tasks? Let me get this straight, assuming you’ve created an eventonCreate
and he calls theAsyncTask
, if clicking the first time works, but if clicking the second does not work? Or you mean that it only works in the first installation?– Guilherme Nascimento
Another doubt, you really need to use
return null;
in a method that usesVoid
?– Guilherme Nascimento
It’s not the same
AsyncTask
because for each one I create a new instance. I have a task to find the location ononCreate
of Activity and a button to send that location for example. In the first installation, the first task of finding the location works. Then I can’t even finish the task ofonCreate
again and not even click the button. Somehow the thread it seems like it’s being busy, keeping others from running, but I have no idea how this is happening, as you can see, my code is pretty simple.– Paulo Rodrigues