Admob 4.1.0 FATAL EXCEPTION: ServiceThread

时间:2023-02-01 21:11:24

过几秒后,广告回来了,但是却出现以下的错误,但是又不能传一个Handler进去,郁闷,有人知道怎么解决的么?

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.Looper.loop(Looper.java:123)
06-30 09:26:50.799: ERROR/AndroidRuntime(1134):     at android.os.HandlerThread.run(HandlerThread.java:60)

早上在床上想到的方法:(经测试可行)

1.先在Activity的onCreate里面,先创建AdView

// 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.让Activity实现接口(implements AdListener)

3.接着在 onReceiveAd里面用Handler来刷新页面

@Override
public void onReceiveAd(Ad arg0) {
if (handler != null ){
handler.sendEmptyMessage(ADS_INCOMMING);
}
}

4.当然,要在Handler处理消息了:

private Handler 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 ;
}
}
}

其中的root就是Acitivity上的Root View,即

@Override
protected void onCreate(Bundle savedInstanceState)
{
...
...
...

setContentView(root);


还有就是1 中的 创建AdView
}

另外,关于无法显示广告的情况,请看下面的帖子: