我尝试在MainActivity之外展示广告。如果我在这个类中调用showAd它很好并且显示我的广告,但是当我通过实现的接口在我的游戏类中调用此函数时,我有一个错误: "需要主线程"
MMRequest request = new MMRequest(); ;
MMInterstitial interstitial;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
MMSDK.initialize(this);
interstitial = new MMInterstitial(this);
interstitial.setMMRequest(request);
interstitial.setApid("xxxxxx");
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
RelativeLayout layout = new RelativeLayout(this);
View gameView = initializeForView(new JumpJackieJump(new RequestHandler(), this), cfg);
layout.addView(gameView);
setContentView(layout); }
@Override
public void showAd()
{
interstitial.fetch();
interstitial.setListener(new RequestListenerImpl()
{
@Override
public void requestCompleted(MMAd mmAd)
{
interstitial.display();
}
});
答案 0 :(得分:0)
这是因为主线程是 UI线程。 (请记住,这仅适用于Android - 有关更多参考,请参阅this)。
根据Android文档,您应该avoid updating the UI outside the UI thread。
Additionally, the Andoid UI toolkit is not thread-safe. So, you must not manipulate your UI from a worker thread—you must do all manipulation to your user interface from the UI thread.
话虽这么说,你可以用this之类的东西做你正在寻找的东西。