title: Integrating ad in Fragment
body: <p>I want to integrate ad to my app, but the problem is that the app crashes, <strong>when I add ads to a fragment</strong> instead of an activity.</p>
  
  <p>Can anyone tell me the working code for it?</p>
  
  <p>I had tried some methods, but they didn't work. If the code is placed in an activity it works correctly</p>
  
  <p>Here is the code I tried for Fragment:</p>
  
  <pre><code>//it shows error in get application content and find view by id
  // so I added get activity() and get view() to it
  
  MobileAds.initialize(getActivity().getApplicationContext(), "ca-app-pub-xxxxxxxxxxxxx~33xxxxxxx");
  AdView mAdView = (AdView) getView().findViewById(R.id.adView);
  AdRequest adRequest = new AdRequest.Builder().build();
  mAdView.loadAd(adRequest);
  </code></pre>