AdMob 4.1.0 fatal exception: servicethread

Source: Internet
Author: User
Tags admob

A few seconds later, the advertisement came back, but the following error occurred, but the handler could not be passed in. Depressed. Does anyone know how to solve this problem?

06-30 09:26:50. 799: Error/androidruntime (1134): Fatal exception: servicethread

06-30 09:26:50. 799: Error/androidruntime (1134): Android. View. viewroot $ calledfromwrongthreadexception: only the original thread that created a view hierarchy can touch its views.
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. View. viewroot. checkthread (viewroot. Java: 2954)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. View. viewroot. requestlayout (viewroot. Java: 645)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. View. View. requestlayout (view. Java: 8397)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. View. View. requestlayout (view. Java: 8397)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. View. View. requestlayout (view. Java: 8397)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. View. View. requestlayout (view. Java: 8397)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. View. View. requestlayout (view. Java: 8397)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. widget. relativelayout. requestlayout (relativelayout. Java: 257)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. View. View. requestlayout (view. Java: 8397)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. View. View. setflags (view. Java: 4690)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. View. View. setvisibility (view. Java: 3165)
06-30 09:26:50. 799: Error/androidruntime (1134): at D. Q (unknown source)
06-30 09:26:50. 799: Error/androidruntime (1134): at C. onpostexecute (unknown source)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. OS. asynctask. Finish (asynctask. Java: 417)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. OS. asynctask. Access $300 (asynctask. Java: 127)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. OS. asynctask $ internalhandler. handlemessage (asynctask. Java: 429)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. OS. handler. dispatchmessage (handler. Java: 99)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. OS. login. loop (login. Java: 123)
06-30 09:26:50. 799: Error/androidruntime (1134): at Android. OS. handlerthread. Run (handlerthread. Java: 60)

Method of thinking in bed in the morning: (tested and feasible)

1. Create an adview in oncreate of the activity.

// Create the adview
// Please replace my_banner_unit_id with your AdMob publisher ID
Madview = new adview (this, adsize. Banner, my_banner_unit_id );
Madview. setadlistener (this );
// Initiate a generic request to load it with an ad
Adrequest ad = new adrequest ();
Madview. loadad (AD );

2. Let the activity implement the interface (implements adlistener)

3. Use handler in onreceivead to refresh the page

@ Override
Public void onreceivead (AD arg0 ){
If (handler! = NULL ){
Handler. sendemptymessage (ads_incomming );
}
}

4. Of course, you have to process the message in handler:

Private handler = new handler (){
@ Override
Public void handlemessage (Message MSG ){
Switch (msg. What ){
Case ads_comming :{
Root. addview (madview, new linearlayout. layoutparams (linearlayout. layoutparams. fill_parent,
Linearlayout. layoutparams. wrap_content ));
Break;
}
}
}

The root is the Root View on acitivity, that is

@ Override
Protected void oncreate (bundle savedinstancestate)
{
...
...
...

Setcontentview (Root );

There is also the creation of adview in 1.
}
In addition, for information about the inability to display advertisements, see the following post:

Analyze why AdMob does not display advertisements in China

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.