2017-04-13 4 views
0

I Kürzlich war erfolgreich bei der Herstellung der Banner-Anzeigen arbeiten. Jetzt habe ich versucht, in einem Fragment die Banneranzeige zu übertragen, und selbst wenn alles zu funktionieren scheint: Die Banneranzeige zeigt nichts.Fragment Banner-Anzeige zeigt keine Anzeigen

Hier die MainActivity:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar myToolbar = (Toolbar) findViewById(R.id.calculator_toobar); 
     setSupportActionBar(myToolbar); 

     // Check that the activity is using the layout version with 
     // the fragment_container FrameLayout 
     if (findViewById(R.id.ads_banner) != null) { 

      // However, if we're being restored from a previous state, 
      // then we don't need to do anything and should return or else 
      // we could end up with overlapping fragments. 
      if (savedInstanceState != null) { 
       return; 
      } 

      // Create a new Fragment to be placed in the activity layout 
      BannerAdsFragment firstFragment = new BannerAdsFragment(); 

      // In case this activity was started with special instructions from an 
      // Intent, pass the Intent's extras to the fragment as arguments 
      firstFragment.setArguments(getIntent().getExtras()); 

      // Add the fragment to the 'fragment_container' FrameLayout 
      getSupportFragmentManager().beginTransaction().add(R.id.ads_banner, firstFragment).commit(); 

     } 
    } 

Hier die main_activity.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="postfixcalculator.mattiarubini.com.postfixcalculator.MainActivity">   

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/ads_banner" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" /> 


    <!--ActionBar--> 
    <android.support.v7.widget.Toolbar 
     android:id="@+id/calculator_toobar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="#f45342" 
     android:elevation="4dp" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

    <TextView 
     android:id="@+id/textEquation" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="26sp" 
     android:padding="10sp" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="Equation" /> 

</LinearLayout> 

die BannerAdsFragment:

public class BannerAdsFragment extends Fragment { 

    private static final String TAG = "MainActivity"; 
    private AdView mAdView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.banner_ad_on_top, null); 
     mAdView = (AdView) rootView.findViewById(R.id.adView); 
     AdRequest adRequest = new AdRequest.Builder().build(); 
     mAdView.loadAd(adRequest); 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.banner_ad_on_top, container, false); 
    } 
} 

Die banner_ads_on_top.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

     <!--Banner ad--> 
     <RelativeLayout xmlns:ads="http://schemas.android.com/apk/res-auto" 
      android:layout_height="50dp" 
      android:layout_width="match_parent"> 
      <com.google.android.gms.ads.AdView 
       android:id="@+id/adView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_alignParentBottom="true" 
       ads:adSize="BANNER" 
       ads:adUnitId="@string/banner_ad_unit_id"> 
      </com.google.android.gms.ads.AdView> 
     </RelativeLayout> 

</LinearLayout> 

Antwort

1

Sie blähen das Layout erneut auf, wodurch die vorherige Ansicht zerstört wird.

statt:

return inflater.inflate(R.layout.banner_ad_on_top, container, false); 

Verwendung:

return rootView; 
+0

Sie meinen Verstand gerettet, vielen Dank! –