Android - admob横幅在设置回可见时崩溃

时间:2015-02-07 18:11:58

标签: android crash admob

我在游戏的顶部显示了一个SMART BANNER,但只希望它显示在前端。

进入游戏后,我打电话给:

GameMainActivity.mAdView.setVisibility (View.INVISIBLE);

这可以找到并继续游戏直到玩家死亡。然后我打电话给:

GameMainActivity.mAdView.setVisibility (View.VISIBLE);

然后它崩溃了。还有比我在这里做的更多吗?

编辑:logcat如下:

02-07 18:44:22.422 19564-19674 / com.onesevenone.simples E / AndroidRuntime:FATAL EXCEPTION:Game Thread     过程:com.onesevenone.simples,PID:19564     android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。             在android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6247)             在android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:902)             在android.view.ViewGroup.invalidateChild(ViewGroup.java:4637)             在android.view.View.invalidateInternal(View.java:11690)             在android.view.View.invalidate(View.java:11654)             在android.view.View.setFlags(View.java:9616)             在android.view.View.setVisibility(View.java:6663)             在com.onesevenone.game.state.GameState.update(GameState.java:63)             在com.onesevenone.simples.GameView.updateAndRender(GameView.java:100)             在com.onesevenone.simples.GameView.run(GameView.java:120)             在java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:0)

似乎你正在从非UI线程进行调用。您可以使用runOnUiThread(Runnable action)方法在UI线程上运行:

context.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            GameMainActivity.mAdView.setVisibility (View.VISIBLE);
        }
    });