我想要adob广告,但我得到java.lang.RuntimeException:无法启动活动ComponentInfo {com.yavuzoktay.deprem / com.yavuzoktay.deprem.MainActivity}:java.lang.IllegalStateException:广告尺寸和广告必须在调用loadAd之前设置单元ID。
MainActivity.java
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
private BottomNavigationView navigation;
private ViewPager viewPager;
private FragmentA fragmentA = new FragmentA();
private FragmentB fragmentB = new FragmentB();
private FragmentC fragmentC = new FragmentC();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
setTitle("Depremler");
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.addOnPageChangeListener(this);
navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return fragmentA;
case 1:
return fragmentB;
case 2:
return fragmentC;
}
return null;
}
@Override
public int getCount() {
return 3;
}
});
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
viewPager.setCurrentItem(item.getOrder());
return true;
}
};
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
navigation.getMenu().getItem(position).setChecked(true);
//setTitle(String.valueOf(position));
switch (position) {
case 0:
setTitle("Depremler");
break;
case 1:
setTitle("Büyük Depremler");
break;
case 2:
setTitle("Önemli Depremler");
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
<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="ca-app-pub-3040537334820006/3047566211">
</com.google.android.gms.ads.AdView>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation"/>
您的建议对我很重要
非常感谢!