Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 这是附加到视图的内存泄漏吗?_Android_Memory Leaks - Fatal编程技术网

Android 这是附加到视图的内存泄漏吗?

Android 这是附加到视图的内存泄漏吗?,android,memory-leaks,Android,Memory Leaks,这是泄漏吗?将闭包连接到textView? countdownTextView是getViewById()和布局视图的一部分。当 void postCountDownTimer(Long countDownTime, LinearLayout countdownContainer, TextView countdownTextView) { if (countDownTime >= System.currentTimeMillis()) { countdownTex

这是泄漏吗?将闭包连接到textView? countdownTextView是getViewById()和布局视图的一部分。当

void postCountDownTimer(Long countDownTime, LinearLayout countdownContainer, TextView countdownTextView) {
    if (countDownTime >= System.currentTimeMillis()) {
        countdownTextView.postDelayed(new Runnable() {
            @Override
            public void run() {
                postCountDownTimer(countDownTime, countdownContainer, countdownTextView);
            }
        }, COUNTDOWN_DELAY_MILLIS);
    }

是的。只要countDownTime>=System.currentTimeMillis()为true,它将持续向UI线程上的处理程序发送消息。这将保留对countdownTextView和countdownContainer的引用,直到调用该函数且条件的计算结果为false为止。您确实希望使用自己的处理程序来执行此操作,这样您就可以删除onStop或onDestroy中的所有消息(最好是onStop,这样您的UI就不会在后台更新),并删除引用,这样它就不会泄漏。

是的,确实如此。只要countDownTime>=System.currentTimeMillis()为true,它将持续向UI线程上的处理程序发送消息。这将保留对countdownTextView和countdownContainer的引用,直到调用该函数且条件的计算结果为false为止。您确实希望使用自己的处理程序来执行此操作,这样您就可以删除onStop或onDestroy中的所有消息(最好是onStop,这样您的UI就不会在后台尝试更新),并删除引用,这样它就不会泄漏。

您应该使用
处理程序()
对于这种情况,因为它会根据您尝试执行的操作导致内存泄漏
postDelayed
将推送到队列中,当上下文被破坏时,队列将留在后面

做一些类似于:

private Handler mHandler = new Handler();

void postCountDownTimer(Long countDownTime, LinearLayout countdownContainer, TextView countdownTextView) {
     if (countDownTime >= System.currentTimeMillis()) {
         mHandler.postDelayed(new Runnable() {
             @Override
             public void run() {
                  mHandler.postDelayed(this, COUNTDOWN_DELAY_MILLIS);
             }
         }, COUNTDOWN_DELAY_MILLIS);
     }
}

//then on somewhere when your context gets destroyed, perform the call below:
mHandler.removeCallbacksAndMessages(null);

对于这种情况,您应该使用
Handler()
,因为它会根据您尝试执行的操作导致内存泄漏
postDelayed
将推送到队列中,当上下文被破坏时,队列将留在后面

做一些类似于:

private Handler mHandler = new Handler();

void postCountDownTimer(Long countDownTime, LinearLayout countdownContainer, TextView countdownTextView) {
     if (countDownTime >= System.currentTimeMillis()) {
         mHandler.postDelayed(new Runnable() {
             @Override
             public void run() {
                  mHandler.postDelayed(this, COUNTDOWN_DELAY_MILLIS);
             }
         }, COUNTDOWN_DELAY_MILLIS);
     }
}

//then on somewhere when your context gets destroyed, perform the call below:
mHandler.removeCallbacksAndMessages(null);