我有一个Surfaceview和一个AdView,我画了这样的广告:
g=new GameView(this );
setContentView(R.layout.activity_game);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.vMain);
layout.addView(g);
mAdView = new AdView(this);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(myId);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
if(mAdView.getAdSize() != null || mAdView.getAdUnitId() != null)
mAdView.loadAd(adRequest);
((RelativeLayout)findViewById(R.id.vMain)).addView(mAdView );
mAdView.bringToFront();
mAdView.setVisibility(View.GONE);
在Surfaceview上,隐藏它。现在我想通过从surfaceview调用它来显示它,这可能吗? 我尝试创建这个函数:
public void hide(){
mAdView.setVisibility(View.GONE);
}
public void show(){
mAdView.setVisibility(View.VISIBLE);
}
并从surfaceview调用它,但它给了我"只有创建视图的原始线程才能触及它的视图。“例外。我该怎么办?
答案 0 :(得分:1)
我不知道您从哪个线程调用这些方法,但必须从主线程调用所有UI更新。使用:
runOnUiThread(new Runnable() {
@Override
public void run() {
// Update you UI here.
}
});
例如:
public void hide(){
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdView.setVisibility(View.GONE);
}
});
}
public void show(){
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdView.setVisibility(View.VISIBLE);
}
});
}