所以我完成了我的Android游戏并决定通过广告将其货币化。但是,我从AdView获得了一些奇怪的行为。我的游戏由2个活动组成,我们称之为活动A和活动B.A是游戏的表面视图,没有.xml文件,B是用于显示高分的常规活动。所以我希望广告出现在A上,但事实并非如此。在Logcat上我得到了请求开始,3分钟后它给出了一个错误代码0(根据我的理解,这意味着有一个内部错误)。奇怪的是,如果在请求开始后我切换到活动B(A没有调用finish(),因此它仍然在后台加载)并等待大约10秒钟我得到'广告完成加载'。然后,如果我完成B并返回A,广告将被加载并正常显示。
总而言之,只有在广告等待回复请求时加载了活动B,活动A的广告才会显示。 在底部的更新中,您将看到一些代码。
我不知道它是否相关,但是活动B确实具有显示广告的“先决条件”(导入,xmlns:ads ...)和在其.xml中声明的AdView但是用于显示广告的java代码广告位于评论栏中。如果您想了解更多信息或希望我进行一些测试,请告诉我。 非常感谢任何帮助。
UPDATE 经过测试,我发现即使我删除了活动B中广告的所有引用,adview仍然会正常显示在A上。另外,删除行'mainLayout.addView(renderView);'修复问题,但显然这不是一个选项。 这里有更多代码。
// Activity A (SurfaceView)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appContext = this.getApplicationContext();
requestWindowFeature(Window.FEATURE_NO_TITLE);
adView = new AdView(this);
adView.setAdUnitId(MY_AD_UNIT_ID);
Builder adRequest = new AdRequest.Builder();
adRequest.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
adView.setAdSize(AdSize.BANNER);
adView.loadAd(adRequest.build());
mainLayout = new FrameLayout(this);
adLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp1 = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp2 = new LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
adLayout.setLayoutParams(lp2);
adLayout.addView(adView);
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
lp1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
adView.setLayoutParams(lp1);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
int frameBufferWidth = isPortrait ? 800 : 1280;
int frameBufferHeight = isPortrait ? 1280 : 800;
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, frameBufferHeight, Config.RGB_565);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float scaleX = (float) frameBufferWidth / metrics.widthPixels;
float scaleY = (float) frameBufferHeight / metrics.heightPixels;
GameScreen.deviceWidth = metrics.widthPixels;
GameScreen.deviceHeight = metrics.heightPixels;
prefs = this.getSharedPreferences("AppPrefs", Context.MODE_PRIVATE);
prefsExist = prefs.contains("showValueAddition");
loadDefaultSettings();
renderView = new AndroidFastRenderView(this, frameBuffer);
graphics = new AndroidGraphics(getAssets(), frameBuffer);
fileIO = new AndroidFileIO(this);
audio = new AndroidAudio(this);
input = new AndroidInput(this, renderView, scaleX, scaleY);
screen = getInitScreen(prefs);
mainLayout.addView(renderView);
mainLayout.addView(adLayout);
setContentView(mainLayout);
}
// Activity B
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.highscoresactivity);
prefs = this.getSharedPreferences("AppPrefs", Context.MODE_PRIVATE);
name[0] = (TextView) findViewById(R.id.tvHighscore1N);
name[1] = (TextView) findViewById(R.id.tvHighscore2N);
name[2] = (TextView) findViewById(R.id.tvHighscore3N);
name[3] = (TextView) findViewById(R.id.tvHighscore4N);
name[4] = (TextView) findViewById(R.id.tvHighscore5N);
value[0] = (TextView) findViewById(R.id.tvHighscore1V);
value[1] = (TextView) findViewById(R.id.tvHighscore2V);
value[2] = (TextView) findViewById(R.id.tvHighscore3V);
value[3] = (TextView) findViewById(R.id.tvHighscore4V);
value[4] = (TextView) findViewById(R.id.tvHighscore5V);
rgGamemode = (RadioGroup) findViewById(R.id.rgGamemode);
rgDifficulty = (RadioGroup) findViewById(R.id.rgDifficulty);
btnClearThis = (Button) findViewById(R.id.btnClearThis);
btnClearAll = (Button) findViewById(R.id.btnClearAll);
rgGamemode.setOnCheckedChangeListener(this);
rgDifficulty.setOnCheckedChangeListener(this);
btnClearThis.setOnClickListener(this);
btnClearAll.setOnClickListener(this);
m = GameMode.Arcade;
d = Difficulty.Easy;
updateTextViews();
/*AdView mAdView = (AdView) findViewById(R.id.adView);
Builder adRequest = new AdRequest.Builder();
adRequest.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
mAdView.loadAd(adRequest.build());*/
}
更新2: 有一个类扩展了A(我忘了提到A的类是抽象的),并且该类被设置为通过清单的启动器。 B在A:
中通过此方法初始化public static void startHighscoreActivity() {
Intent i = new Intent("android.intent.action.HIGHSCORE");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity(i);
}