This handler class shocould be static or leaks might occur

Source: Internet
Author: User

Original code:

Private handler mhandler = new handler (){
@ Override
Public void handlemessage (Message MSG ){
Switch (msg. What ){
Case get_current_time:

Intent actiongetduration = new intent ("x1.getduration ");
Sendbroadcast (actiongetduration );
Msumtv. settext (helper. sumduration );
Mnowtv. settext (helper. currentduration );

If (helper. currentsecond = helper. sumsecond) // The playback is complete.
{
If (helper. currentsecond = 0)
{
Log. D (TAG, "In currentsecond = 0 ");
}
}
Else
{

Log. D (TAG, "In set progress ");

Log. D (TAG, "helper. sumsecond =" + helper. sumsecond );
Long position = mseekbar. getmax () * helper. currentsecond/helper. sumsecond;
Mseekbar. setprogress (INT) position );
}

Break;
}
}

};

Modified code:

Static class myhandler extends handler {
Weakreference <browseractivity> mactivity;

Myhandler (browseractivity activity ){
Mactivity = new weakreference <browseractivity> (activity );
}

@ Override
Public void handlemessage (Message MSG ){
Browseractivity theactivity = mactivity. Get ();
Switch (msg. What ){
Case get_current_time:

Intent actiongetduration = new intent ("x1.getduration ");
Theactivity. sendbroadcast (actiongetduration );
Theactivity. msumtv. settext (helper. sumduration );
Theactivity. mnowtv. settext (helper. currentduration );

If (helper. currentsecond = helper. sumsecond) // The playback is complete.
{
If (helper. currentsecond = 0)
{
Log. D (TAG, "In currentsecond = 0 ");
}
}
Else
{

Log. D (TAG, "In set progress ");

Log. D (TAG, "helper. sumsecond =" + helper. sumsecond );
Long position = theactivity. mseekbar. getmax () * helper. currentsecond/helper. sumsecond;
Theactivity. mseekbar. setprogress (INT) position );
}

Break;

}
}
};

Private myhandler mhandler = new myhandler (this );

The warning in the location where the mhandler is defined disappears, but the error fatal signal 6 (SIGABRT) at 0x00000138 (code = 0) still occurs, it is reported in libc, and the program crashes. the ANR error is displayed, and the reason is: keydispatchingtimedout. Continue to solve the bug. I really don't know where the problem is.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.