AsyncTask onPostExecute()有时不会被调用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AsyncTask onPostExecute()有时不会被调用相关的知识,希望对你有一定的参考价值。

我有一个AsynTask来请求数据和更新UI,但是当我调用任务时,有时候不会调用onPostExecute()。

我添加了log,来检查执行流程,当没有调用onPostExecute时,最后一个日志是doInBacground()返回之前的那个 - > - > 应用程序冻结有没有人经历过类似的事情?

    public class dataLoadTask extends AsyncTask<Boolean, Void, Boolean> {    
        JSONObject token;    
        public dataLoadTask(JSONObject token) {
            if (token != null) {
                this.token = token;
            }
        }    
        @Override
        protected Boolean doInBackground(Boolean... params) {
            JSONArray response;
            Boolean result = false;
            try {                    
                loadingData = true;
                response = NetworkUtils.schoolNotifications(token);                    
                notifications = parseResponse(response);
                Collections.sort(notifications, new NotificationsComparator());
                loadingData = false;                       
                JSONArray chatsJSON = NetworkUtils.schoolChats(token);                    
                chats = new ArrayList<>();
                for (int i = 0; i < chatsJSON.length(); i++) {
                    JSONObject json_chat = (JSONObject) chatsJSON.get(i);
                    Chat chat = new Chat(json_chat);
                    chats.add(chat);    
                }
                Collections.sort(chats, new ChatsComparator());                        
                result = true;                           
            } catch (Exception e) {                   
                e.printStackTrace();
            }                    
            return result;       
        }              
        @Override
        protected void onPostExecute(Boolean result) {                
            if (result) {                    
                bottomNavigation.setOnNavigationItemSelectedListener
                        (new BottomNavigationView.OnNavigationItemSelectedListener() {
                            @Override
                            public boolean onNavigationItemSelected(MenuItem item) {
                                Fragment selectedFragment = null;
                                String fragmentTag = "";                                    getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                                switch (item.getItemId()) {
                                    case R.id.action_home:
                                        selectedFragment = HomeFragment.newInstance();
                                        myBundle.putParcelableArrayList("notifications", notifications);
                                        myBundle.putParcelableArrayList("chats", chats);
                                        myBundle.putParcelableArrayList("schedule", schedule);
                                        fragmentTag = HOME_TAG;
                                        break;
                                    case R.id.action_subjects:
                                        selectedFragment = SubjectsFragment.newInstance();
                                        myBundle.putParcelableArrayList("notifications", notifications);
                                        myBundle.putParcelableArrayList("subjects", subjects);
                                        break;
                                    case R.id.action_tasks:
                                        selectedFragment = TasksFragment.newInstance();
                                        myBundle.putParcelableArrayList("notifications", notifications);
                                        break;
                                    case R.id.action_chats:
                                        selectedFragment = ChatsFragment.newInstance();
                                        myBundle.putParcelableArrayList("users", users);
                                        myBundle.putParcelableArrayList("chats", chats);
                                        fragmentTag = CHAT_TAG;
                                        break;
                                    case R.id.action_new_notification:
                                        selectedFragment = NewNotificationFragment.newInstance();
                                        myBundle.putParcelableArrayList("classes", classes);

                                        break;    
                                }                                    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                                selectedFragment.setArguments(myBundle);
                                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                                transaction.replace(R.id.frame_layout, selectedFragment, fragmentTag);
                                transaction.commit();
                                return true;
                            }
                        });                   
                myBundle.putParcelableArrayList("notifications", notifications);
                myBundle.putParcelableArrayList("chats", chats);
                myBundle.putParcelableArrayList("schedule", schedule);
                myBundle.putBoolean("startup", false);
                Log.i("TESTTTTTTTTTTT", "9");
                HomeFragment activeFragment = (HomeFragment) getSupportFragmentManager().findFragmentByTag(HOME_TAG);
                Boolean homeActive = activeFragment != null && activeFragment.isVisible() ? true : false;
                if (!loadingNewData || (loadingNewData && homeActive)) {
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    HomeFragment homeFragment = HomeFragment.newInstance();
                    homeFragment.setArguments(myBundle);
                    transaction.replace(R.id.frame_layout, homeFragment, HOME_TAG);
                    transaction.commit();
                    progressBar.setVisibility(View.INVISIBLE);
                    bottomNavigation.setEnabled(true);
                    profilePhoto.setEnabled(true);
                } else
                    loadingNewData = false;                
                ChatsFragment activeChatsFragment = (ChatsFragment) getSupportFragmentManager().findFragmentByTag(CHAT_TAG);
                Boolean chatsActive = activeChatsFragment != null && activeChatsFragment.isVisible() ? true : false;
                if (chatsActive) {
                    activeChatsFragment.updateAdapter(chats);
                }                   
            } else {
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
            }               
            loadDataProcessRunning = false;    
        }
    }

日志:

12-22 12:23:27.916 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 01
12-22 12:23:27.916 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 1
12-22 12:23:29.549 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 2
12-22 12:23:29.560 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 3
12-22 12:23:29.668 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 4
12-22 12:23:29.668 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 5
12-22 12:23:29.669 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 55
12-22 12:23:29.669 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: true
12-22 12:23:29.681 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 6
12-22 12:23:29.682 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 7
12-22 12:23:29.682 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 8
12-22 12:23:29.682 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 9
12-22 12:23:29.682 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 10
12-22 12:23:29.683 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 11
12-22 12:23:29.684 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 12
12-22 12:23:29.684 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 13
12-22 12:23:29.748 15252-15252/com.cathedralsw.schoolteacher E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/user/0/com.cathedralsw.schoolteacher/files/5 (No such file or directory)
12-22 12:23:29.876 15252-15252/com.cathedralsw.schoolteacher E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/user/0/com.cathedralsw.schoolteacher/files/5 (No such file or directory)
12-22 12:23:29.881 15252-15252/com.cathedralsw.schoolteacher E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/user/0/com.cathedralsw.schoolteacher/files/5 (No such file or directory)
12-22 12:23:29.883 15252-15257/com.cathedralsw.schoolteacher I/art: Do partial code cache collection, code=23KB, data=30KB
12-22 12:23:29.884 15252-15257/com.cathedralsw.schoolteacher I/art: After code cache collection, code=23KB, data=30KB
12-22 12:23:29.884 15252-15257/com.cathedralsw.schoolteacher I/art: Increasing code cache capacity to 128KB
12-22 12:23:29.887 15252-15252/com.cathedralsw.schoolteacher E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/user/0/com.cathedralsw.schoolteacher/files/5 (No such file or directory)
12-22 12:23:29.910 15252-15252/com.cathedralsw.schoolteacher E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/user/0/com.cathedralsw.schoolteacher/files/5 (No such file or directory)
12-22 12:23:29.924 15252-15252/com.cathedralsw.schoolteacher E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/user/0/com.cathedralsw.schoolteacher/files/6 (No such file or directory)
12-22 12:23:34.445 15252-15252/com.cathedralsw.schoolteacher I/hwaps: JNI_OnLoad
12-22 12:23:35.168 15252-15257/com.cathedralsw.schoolteacher I/art: Do partial code cache collection, code=52KB, data=60KB
12-22 12:23:35.169 15252-15257/com.cathedralsw.schoolteacher I/art: After code cache collection, code=51KB, data=60KB
12-22 12:23:35.169 15252-15257/com.cathedralsw.schoolteacher I/art: Increasing code cache capacity to 256KB
12-22 12:23:35.530 15252-15252/com.cathedralsw.schoolteacher E/RecyclerView: No adapter attached; skipping layout
12-22 12:23:35.575 15252-15252/com.cathedralsw.schoolteacher I/Choreographer: Skipped 57 frames!  The application may be doing too much work on its main thread.
12-22 12:23:35.886 15252-15279/com.cathedralsw.schoolteacher I/FA: Tag Manager is not found and thus will not be used
12-22 12:23:36.899 15252-15252/com.cathedralsw.schoolteacher I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
12-22 12:23:38.361 15252-15252/com.cathedralsw.schoolteacher I/HwPointEventFilter: do not support AFT because of no config
12-22 12:23:41.346 15252-15252/com.cathedralsw.schoolteacher I/HwSecImmHelper: mSecurityInputMethodService is null
12-22 12:23:41.361 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:41.361 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:41.372 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:41.372 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:42.755 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
12-22 12:23:42.759 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:42.759 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:42.766 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:42.766 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:44.760 15252-15252/com.cathedralsw.schoolteacher I/HwSecImmHelper: mSecurityInputMethodService is null
12-22 12:23:44.841 15252-15252/com.cathedralsw.schoolteacher I/HwPointEventFilter: do not support AFT because of no config
12-22 12:23:46.033 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 01
12-22 12:23:46.034 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 1
12-22 12:23:46.059 15252-15252/com.cathedralsw.schoolteacher I/HwSecImmHelper: mSecurityInputMethodService is null
12-22 12:23:47.798 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 2
12-22 12:23:47.808 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 3
12-22 12:23:47.820 15252-15252/com.cathedralsw.schoolteacher I/Choreographer: Skipped 106 frames!  The application may be doing too much work on its main thread.
12-22 12:23:47.902 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 4
12-22 12:23:47.903 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 5
12-22 12:23:47.903 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 55
12-22 12:23:47.903 15252-15292/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: true
12-22 12:23:48.551 15252-15257/com.cathedralsw.schoolteacher I/art: Do full code cache collection, code=122KB, data=123KB
12-22 12:23:48.552 15252-15257/com.cathedralsw.schoolteacher I/art: After code cache collection, code=100KB, data=86KB
12-22 12:23:48.644 15252-15252/com.cathedralsw.schoolteacher I/HwPointEventFilter: do not support AFT because of no config
12-22 12:23:48.655 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
12-22 12:23:48.656 15252-15252/com.cathedralsw.schoolteacher I/Choreographer: Skipped 49 frames!  The application may be doing too much work on its main thread.
12-22 12:23:48.720 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 6
12-22 12:23:48.720 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 7
12-22 12:23:48.720 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 8
12-22 12:23:48.720 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 9
12-22 12:23:48.720 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 10
12-22 12:23:48.720 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 11
12-22 12:23:48.720 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 12
12-22 12:23:48.720 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 13
12-22 12:23:48.720 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
12-22 12:23:48.747 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
12-22 12:23:48.751 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
12-22 12:23:48.752 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
12-22 12:23:48.752 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
12-22 12:23:48.753 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
12-22 12:23:48.753 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
12-22 12:23:48.755 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
12-22 12:23:48.755 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
12-22 12:23:48.755 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
12-22 12:23:48.756 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
12-22 12:23:53.903 15252-15252/com.cathedralsw.schoolteacher I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
12-22 12:23:55.415 15252-15252/com.cathedralsw.schoolteacher I/HwPointEventFilter: do not support AFT because of no config
12-22 12:23:56.601 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
12-22 12:23:57.745 15252-15252/com.cathedralsw.schoolteacher I/HwSecImmHelper: mSecurityInputMethodService is null
12-22 12:23:57.756 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:57.756 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:57.762 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:57.762 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:59.740 15252-15252/com.cathedralsw.schoolteacher W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
12-22 12:23:59.744 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:59.744 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:59.749 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:23:59.750 15252-15252/com.cathedralsw.schoolteacher E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-22 12:24:00.910 15252-15252/com.cathedralsw.schoolteacher I/HwSecImmHelper: mSecurityInputMethodService is null
12-22 12:24:00.985 15252-15252/com.cathedralsw.schoolteacher I/HwPointEventFilter: do not support AFT because of no config
12-22 12:24:02.687 15252-15252/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 01
12-22 12:24:02.687 15252-15298/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 1
12-22 12:24:02.707 15252-15252/com.cathedralsw.schoolteacher I/HwSecImmHelper: mSecurityInputMethodService is null
12-22 12:24:04.603 15252-15298/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 2
12-22 12:24:04.611 15252-15298/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 3
12-22 12:24:04.703 15252-15298/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 4
12-22 12:24:04.704 15252-15298/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 5
12-22 12:24:04.704 15252-15298/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: 55
12-22 12:24:04.704 15252-15298/com.cathedralsw.schoolteacher I/TESTTTTTTTTTTT: true

应用程序冻结,最后一个日志来自doInBackground()...上次调用中没有来自onPostExecute()的日志

答案

如果你的android api版本超过14,用Asynctask执行你的executor

因为在较低版本的Android中,所有AsyncTasks都是在单个后台线程上执行的。所以新任务可能正在等待,直到其他任务正常工作。

在Android的较低版本中(实际上在pre-HONEYCOMB上),您无法在执行程序上执行AsyncTask。

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
     task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
 else
     task.execute();

在HONEYCOMB execute()方法之前并行运行AsynkTask。

以上是关于AsyncTask onPostExecute()有时不会被调用的主要内容,如果未能解决你的问题,请参考以下文章

完成 AsyncTask 后未调用 onPostExecute

AsyncTask 的 onPostExecute 被调用:但 doInBackground 没有

onPostExecute() 无法重新运行从活动调用的 asyncTask

Android,AsyncTask 不调用 onProgressUpdate 和 onPostExecute

AsyncTask中onPostExecute方法的返回类型可以不为void吗?

从Asynctask ONPostExecute调用片段方法