Admob 与 AppCompatActivity 的集成

Admob integration with AppCompatActivity

我正在努力将 admob 添加到我基于 AppCompatActivity 的应用程序中。我已经尝试了几种方法,例如自动生成的方式(admob),但有些方法没有成功。下面主要activityclass供参考

 public class WorldClockActivity extends AppCompatActivity {
    private static final boolean IS_GINGERBREAD = Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;

      /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentManager fm = getSupportFragmentManager();
        // Create the list fragment and add it as our sole content.
        if (fm.findFragmentById(android.R.id.content) == null) {
            ClockListFragment list = new ClockListFragment();
            fm.beginTransaction().add(android.R.id.content, list).commit();
        }
        //try to add any other view
MobileAds.initialize(this,
                "ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx");

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

与其他片段 ClockListFragment activity 代码如下。

public static class ClockListFragment extends ListFragment implements
        LoaderManager.LoaderCallbacks<Cursor>, PauseSource {

    private CursorAdapter mAdapter;
    private ActionMode mMode;
    private OnSharedPreferenceChangeListener mSpChange;
    private boolean mAutoSortClocks;
    private final List<PauseListener> mListeners = new ArrayList<>();

不确定我是否犯了愚蠢的错误。任何帮助将不胜感激。

我可以通过将 android.R.id.content 替换为 基本布局

来修复它
setContentView(R.layout.base_layout_to_bedeleted);
    MobileAds.initialize(this, "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxx");
    mAdView = (AdView)findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    FragmentManager fm = getSupportFragmentManager();
    // Create the list fragment and add it as our sole content.
    //if (fm.findFragmentById(android.R.id.content) == null) {
        ClockListFragment list = new ClockListFragment();
        //fm.beginTransaction().add(android.R.id.content, list).commit();
        fm.beginTransaction().add(R.id.content_pane, list).commit();

DownVoters 有什么理由投反对票?这对我一点帮助都没有。