0

所以,我有单独的绘图类,我想在那里画 adMob,我从游戏类中调用它并将活动作为参数传递:

public void drawAddMob(Activity a){
LinearLayout ll = new LinearLayout(a);
ll.setPadding(0, 50, 0, 0);
adView = new AdView(a, AdSize.BANNER,  "a15140cd49b6e7b");  
AdRequest request = new AdRequest();
adView.loadAd(request);
adView.setVisibility(View.VISIBLE);

ll.addView(adView);

ll.draw(canvas);
}

但是当我这样做时,我得到了错误:

03-21 23:35:09.646: W/System.err(333):
java.lang.RuntimeException: Can't create handler 
inside thread that has not called Looper.prepare()
03-21 23:35:09.656: W/System.err(333):
at com.google.ads.internal.AdWebView.<init>  (SourceFile:69)
4

1 回答 1

1

您需要在 UI 线程上运行此代码,因为它会改变您的 UI。为您的方法主体尝试以下操作:

a.runOnUiThread(new Runnable() {
    public void run() {
        LinearLayout ll = new LinearLayout(a);
        ll.setPadding(0, 50, 0, 0);
        adView = new AdView(a, AdSize.BANNER,  "a15140cd49b6e7b");  
        AdRequest request = new AdRequest();
        adView.loadAd(request);
        adView.setVisibility(View.VISIBLE);

        ll.addView(adView);

        ll.draw(canvas);
    }
});
于 2013-03-21T21:51:27.323 回答