Reputation: 89
I'm working on a simple app that will send POST data to a php script, then take the JSON response and put it into a listview. I have the UI things working, and I get a JSON response from the script, but my app crashes and the debug logs show only a fatal error: main
, pointing towards only my AsyncTask. Is my JSON just not being parsed correctly?
Upvotes: 2
Views: 275
Reputation: 9574
Step 1 credits @Weibo
Move this to your onCreate, doInBackground cannot do any ui stuff. Its the basic rule of AsyncTask. If you must, move the AsyncTask class into your activity where it is needed. Ensure that you dont make it static, since you need the findViewById
of the onCreate.
final Spinner numPoke = (Spinner) findViewById(R.id.SpinnerNumPokemon);
final Spinner dexRegion = (Spinner) findViewById(R.id.SpinnerRegion);
final Spinner pokeType = (Spinner) findViewById(R.id.SpinnerType);
final ToggleButton incNFE = (ToggleButton) findViewById(R.id.toggleNFE);
final ToggleButton incLegendary = (ToggleButton) findViewById(R.id.toggleLegendary);
Step 2
In your onPostExecute, the first statement should have
super.doPostExecute(result) ;
Your list
or pokemonList
is null, check this out. Use Toast's
for debugging.
From the Android documentation, please read the 4 steps.
When an asynchronous task is executed, the task goes through 4 steps:
- onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
- doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
- onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
- onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
Upvotes: 2