我做了一個免費的安卓遊戲,我想爲它添加一個橫幅。我跟着this tutorial,我不知道爲什麼它不起作用。廣告不可見。不令人耳目一新的廣告
當我在LogCat的模擬器上運行我的遊戲時,我看到一條消息「廣告不可見,不刷新廣告」。當我在手機上安裝遊戲時,我也看不到任何廣告。
這裏是我的main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="hidden"
ads:adSize="BANNER"/>
</LinearLayout>
,這裏是在MainActivity.java代碼
public class MainActivity extends AndroidApplication {
private AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = true;
cfg.useAccelerometer = true;
cfg.useCompass = true;
setContentView(R.layout.main);
// Look up the AdView as a resource and load a request.
adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
initialize (new RedSquare(), cfg);
}
@Override
public void onPause() {
adView.pause();
super.onPause();
}
@Override
public void onResume() {
super.onResume();
adView.resume();
}
@Override
public void onDestroy() {
adView.destroy();
super.onDestroy();
}
}
感謝您的答覆。我是新來的Java,我不知道如何解決這個問題...當我嘗試運行你的代碼時,出現了一些錯誤,inflater和getActivity()都是未定義的。你能否寫下我應該把它放到哪裏以便毫無錯誤地編譯它。 – armin
看到我的編輯,希望它有幫助。 – nKn
現在它編譯沒有問題,但是當我嘗試運行遊戲後它開始後崩潰。 :( – armin