Note that there are some explanatory texts on larger screens.

plurals
  1. POincomplete asynctask crashes my app
    primarykey
    data
    text
    <p>I am using <code>AsyncTask</code> in some of my <code>Fragment</code> and <code>Activity</code> and these work nice. But the problem is when I press back button or exit app at the time of <code>Asynchronous</code> task execution, The app crashes. This is my log cat:</p> <pre><code>08-16 08:15:34.032: E/AndroidRuntime(21957): FATAL EXCEPTION: main 08-16 08:15:34.032: E/AndroidRuntime(21957): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 08-16 08:15:34.032: E/AndroidRuntime(21957): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327) 08-16 08:15:34.032: E/AndroidRuntime(21957): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1338) 08-16 08:15:34.032: E/AndroidRuntime(21957): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595) 08-16 08:15:34.032: E/AndroidRuntime(21957): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574) 08-16 08:15:34.032: E/AndroidRuntime(21957): at me.kaidul.uhunt.MainActivity.selectItem(MainActivity.java:434) 08-16 08:15:34.032: E/AndroidRuntime(21957): at me.kaidul.uhunt.MainActivity.access$0(MainActivity.java:387) 08-16 08:15:34.032: E/AndroidRuntime(21957): at me.kaidul.uhunt.MainActivity$GetProblemListTask.onPostExecute(MainActivity.java:680) 08-16 08:15:34.032: E/AndroidRuntime(21957): at me.kaidul.uhunt.MainActivity$GetProblemListTask.onPostExecute(MainActivity.java:1) 08-16 08:15:34.032: E/AndroidRuntime(21957): at android.os.AsyncTask.finish(AsyncTask.java:417) 08-16 08:15:34.032: E/AndroidRuntime(21957): at android.os.AsyncTask.access$300(AsyncTask.java:127) 08-16 08:15:34.032: E/AndroidRuntime(21957): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 08-16 08:15:34.032: E/AndroidRuntime(21957): at android.os.Handler.dispatchMessage(Handler.java:99) 08-16 08:15:34.032: E/AndroidRuntime(21957): at android.os.Looper.loop(Looper.java:123) 08-16 08:15:34.032: E/AndroidRuntime(21957): at android.app.ActivityThread.main(ActivityThread.java:4627) 08-16 08:15:34.032: E/AndroidRuntime(21957): at java.lang.reflect.Method.invokeNative(Native Method) 08-16 08:15:34.032: E/AndroidRuntime(21957): at java.lang.reflect.Method.invoke(Method.java:521) 08-16 08:15:34.032: E/AndroidRuntime(21957): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 08-16 08:15:34.032: E/AndroidRuntime(21957): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 08-16 08:15:34.032: E/AndroidRuntime(21957): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>I am not adding here the <code>AsyncTask</code> code as they are working properly without pressing back button or exiting the app. I call all the <code>AsyncTask</code> like new <code>GetTaskDone().execute(parameter)</code> and there are <code>onPostExecute</code> in every <code>AsycTask</code>.</p> <p>How can I avoid app crashing ?</p> <p><strong>Edit:</strong></p> <p>Now I am thinking that, the problem might not in <code>AsyncTask</code>. Because my other <code>AsyncTask</code> is working regardless of backpress/exit.</p> <p>This is the problematic AsyncTask:</p> <p>protected class GetProblemListTask extends AsyncTask {</p> <pre><code>@Override protected Void doInBackground(String... params) { InputStreamReader isr = null; if (hasConnection) { Date date = new Date(); long savedTime = prefs.getLong(CommonUtils.LAST_SAVED, date.getTime()); long now = date.getTime(); if (now - savedTime &gt; fiveDays || (now - savedTime &lt;= fiveDays &amp;&amp; prefs.getBoolean( CommonUtils.problemListisCached, false) == false)) { if (CommonUtils.isDebuggable) { Log.d("updating", "need to update!"); } prefs.edit() .putBoolean(CommonUtils.problemListisCached, false) .commit(); isr = new JSONDownloader().getJSONStringFromUrl(params[0]); BufferedReader br = new BufferedReader(isr, bufferSize); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = br.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { Log.d("problem", "in file writting"); } writeToFile(sb.toString(), CommonUtils.FILE_PROBLEM_LIST); prefs.edit() .putBoolean(CommonUtils.problemListisCached, true) .commit(); try { isr.close(); } catch (IOException e) { if (CommonUtils.isDebuggable) { Log.d("isr", e.toString()); } } try { br.close(); } catch (IOException e) { if (CommonUtils.isDebuggable) { Log.d("br", e.toString()); } } prefs.edit().putLong(CommonUtils.LAST_SAVED, now).commit(); } else { if (CommonUtils.isDebuggable) { Log.d("old_copy", "Old copy is rendering"); } try { isr = new InputStreamReader( openFileInput(CommonUtils.FILE_PROBLEM_LIST)); } catch (FileNotFoundException e) { Log.d("file_not_found", "File is missing!"); } } } else { try { isr = new InputStreamReader( openFileInput(CommonUtils.FILE_PROBLEM_LIST)); } catch (FileNotFoundException e) { Log.d("file_not_found", "File is missing!"); } } if (CommonUtils.isDebuggable) { Log.d("start", "mapping start"); } try { isr = new InputStreamReader( openFileInput(CommonUtils.FILE_PROBLEM_LIST)); } catch (FileNotFoundException e1) { } JsonReader reader = new JsonReader(isr); try { reader.beginArray(); while (reader.hasNext()) { reader.beginArray(); problems.put( reader.nextInt(), new Problems(reader.nextString(), reader .nextString(), reader.nextInt())); while (reader.hasNext()) reader.skipValue(); reader.endArray(); } reader.endArray(); reader.close(); } catch (IOException e) { if (CommonUtils.isDebuggable) { Log.d("problems", "hashmaping problem"); } } return null; } @Override protected void onPostExecute(Void result) { getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); // this is selecItem() function. It's task is to add a fragment to the activity. selectItem(0); if (CommonUtils.isDebuggable) { Log.d("successful", "eventually survived!"); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload