Admob与AppCompatActivity集成

时间:2018-09-02 03:03:53

标签: android admob fragment android-listfragment

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

 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 活动代码。

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<>();

不确定我在犯傻错误。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我可以通过将 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是否有拒绝投票的任何理由?这根本没有帮助我。