Код работает нормально без части admob. Когда я добавляю код части admob, он показывает ошибку. Как я могу это исправить?
Я добавил две части кода. Одна из этих xml и других частей имеет код Java. Так было бы легче найти мою проблему.
Вот мой xml-код.
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "horizontal">
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_marginBottom = "70dp"
android:gravity = "center"
android:orientation = "horizontal">
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_marginLeft = "@dimen/difficulty_margin"
android:layout_marginTop = "30dp"
android:layout_marginRight = "@dimen/difficulty_margin"
android:layout_marginBottom = "30dp"
android:layout_weight = "1"
android:gravity = "center"
android:orientation = "vertical">
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id = "@+id/select_difficulty_1"
android:layout_width = "match_parent"
android:layout_height = "30dp"
android:layout_weight = "1" />
<TextView
android:id = "@+id/time_difficulty_1"
android:layout_width = "match_parent"
android:layout_height = "35dp"
android:layout_marginTop = "-40dp"
android:layout_marginBottom = "18dp"
android:background = "@drawable/time_border"
android:textSize = "17sp" />
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id = "@+id/select_difficulty_4"
android:layout_width = "match_parent"
android:layout_height = "30dp"
android:layout_weight = "1" />
<TextView
android:id = "@+id/time_difficulty_4"
android:layout_width = "match_parent"
android:layout_height = "35dp"
android:layout_marginTop = "-40dp"
android:background = "@drawable/time_border"
android:textSize = "17sp" />
</LinearLayout>
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_marginLeft = "@dimen/difficulty_margin"
android:layout_marginTop = "30dp"
android:layout_marginRight = "@dimen/difficulty_margin"
android:layout_marginBottom = "30dp"
android:layout_weight = "1"
android:gravity = "center"
android:orientation = "vertical">
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id = "@+id/select_difficulty_2"
android:layout_width = "match_parent"
android:layout_height = "30dp"
android:layout_weight = "1" />
<TextView
android:id = "@+id/time_difficulty_2"
android:layout_width = "match_parent"
android:layout_height = "35dp"
android:layout_marginTop = "-40dp"
android:layout_marginBottom = "18dp"
android:background = "@drawable/time_border"
android:textSize = "17sp" />
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id = "@+id/select_difficulty_5"
android:layout_width = "match_parent"
android:layout_height = "30dp"
android:layout_weight = "1" />
<TextView
android:id = "@+id/time_difficulty_5"
android:layout_width = "match_parent"
android:layout_height = "35dp"
android:layout_marginTop = "-40dp"
android:background = "@drawable/time_border"
android:textSize = "17sp" />
</LinearLayout>
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_marginLeft = "@dimen/difficulty_margin"
android:layout_marginTop = "30dp"
android:layout_marginRight = "@dimen/difficulty_margin"
android:layout_marginBottom = "30dp"
android:layout_weight = "1"
android:gravity = "center"
android:orientation = "vertical">
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id = "@+id/select_difficulty_3"
android:layout_width = "match_parent"
android:layout_height = "30dp"
android:layout_weight = "1" />
<TextView
android:id = "@+id/time_difficulty_3"
android:layout_width = "match_parent"
android:layout_height = "35dp"
android:layout_marginTop = "-40dp"
android:layout_marginBottom = "18dp"
android:background = "@drawable/time_border"
android:textSize = "17sp" />
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id = "@+id/select_difficulty_6"
android:layout_width = "match_parent"
android:layout_height = "30dp"
android:layout_weight = "1" />
<TextView
android:id = "@+id/time_difficulty_6"
android:layout_width = "match_parent"
android:layout_height = "35dp"
android:layout_marginTop = "-40dp"
android:background = "@drawable/time_border"
android:textSize = "17sp" />
</LinearLayout>
</LinearLayout>
<com.google.android.gms.ads.AdView xmlns:ads = "http://schemas.android.com/apk/res-auto"
android:id = "@+id/adView"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_alignParentLeft = "true"
android:layout_alignParentBottom = "true"
android:layout_marginTop = "@dimen/margintoptendp"
ads:adSize = "BANNER"
ads:adUnitId = "@string/banner_adUnitId" />
</LinearLayout>
Также я добавил код части Java. Это также мне поможет исправить это.
DifficultySelectFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = LayoutInflater.from(Shared.context).inflate(R.layout.difficulty_select_fragment, container, false);
Theme theme = Shared.engine.getSelectedTheme();
DifficultyView difficulty1 = view.findViewById(R.id.select_difficulty_1);
difficulty1.setDifficulty(1, Memory.getHighStars(theme.id, 1));
setOnClick(difficulty1, 1);
/* FIrstFragment fIrstFragment = new FIrstFragment();
moveToFragment(fIrstFragment);*/
DifficultyView difficulty2 = view.findViewById(R.id.select_difficulty_2);
difficulty2.setDifficulty(2, Memory.getHighStars(theme.id, 2));
setOnClick(difficulty2, 2);
DifficultyView difficulty3 = view.findViewById(R.id.select_difficulty_3);
difficulty3.setDifficulty(3, Memory.getHighStars(theme.id, 3));
setOnClick(difficulty3, 3);
DifficultyView difficulty4 = view.findViewById(R.id.select_difficulty_4);
difficulty4.setDifficulty(4, Memory.getHighStars(theme.id, 4));
setOnClick(difficulty4, 4);
DifficultyView difficulty5 = view.findViewById(R.id.select_difficulty_5);
difficulty5.setDifficulty(5, Memory.getHighStars(theme.id, 5));
setOnClick(difficulty5, 5);
DifficultyView difficulty6 = view.findViewById(R.id.select_difficulty_6);
difficulty6.setDifficulty(6, Memory.getHighStars(theme.id, 6));
setOnClick(difficulty6, 6);
animate(difficulty1, difficulty2, difficulty3, difficulty4, difficulty5, difficulty6);
Typeface type = Typeface.createFromAsset(Shared.context.getAssets(), "fonts/grobold.ttf");
TextView text1 = view.findViewById(R.id.time_difficulty_1);
text1.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text1.setTypeface(type);
text1.setText(getBestTimeForStage(theme.id, 1));
TextView text2 = view.findViewById(R.id.time_difficulty_2);
text2.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text2.setTypeface(type);
text2.setText(getBestTimeForStage(theme.id, 2));
TextView text3 = view.findViewById(R.id.time_difficulty_3);
text3.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text3.setTypeface(type);
text3.setText(getBestTimeForStage(theme.id, 3));
TextView text4 = view.findViewById(R.id.time_difficulty_4);
text4.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text4.setTypeface(type);
text4.setText(getBestTimeForStage(theme.id, 4));
TextView text5 = view.findViewById(R.id.time_difficulty_5);
text5.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text5.setTypeface(type);
text5.setText(getBestTimeForStage(theme.id, 5));
TextView text6 = view.findViewById(R.id.time_difficulty_6);
text6.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text6.setTypeface(type);
text6.setText(getBestTimeForStage(theme.id, 6));
mAdview = view.findViewById( R.id.adView );
AdRequest adRequest = new AdRequest.Builder().tagForChildDirectedTreatment(true).build();
mAdview.loadAd( adRequest );
return view;
}
Чтобы ответить на ваш вопрос
только в макете не показывается реклама
Объявления не показываются по двум причинам.
1) Если вы только что создали свой идентификатор рекламного моба, велика вероятность, что вы не получите никакой рекламы. Потому что, когда вы создаете новый идентификатор рекламного моба, иногда требуется предоставить некоторую рекламу вашему идентификатору.
2) Если первый случай недействителен, вы сделали ID 4-5 часов назад, а затем убедитесь, что ваш ID правильный. По ошибке вы не используете неправильный идентификатор или опечатку.
Добавьте прослушиватель adview, чтобы узнать, в чем ошибка.
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
Toast.makeText(this, "ad loaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
Toast.makeText(this, errorCode.toString, Toast.LENGTH_SHORT).show();
}
@Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
Toast.makeText(this, "Ad opened", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
Toast.makeText(this, "Ad left application", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdClosed() {
// Code to be executed when when the user is about to return
// to the app after tapping on an ad.
Toast.makeText(this, "ad closed", Toast.LENGTH_SHORT).show();
}
});
проверьте мой обновленный ответ и дайте мне знать, получаете ли вы тост
<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:ads = "http://schemas.android.com/apk/res-auto"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "horizontal">
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_marginBottom = "70dp"
android:gravity = "center"
android:orientation = "horizontal">
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_marginLeft = "@dimen/difficulty_margin"
android:layout_marginTop = "30dp"
android:layout_marginRight = "@dimen/difficulty_margin"
android:layout_marginBottom = "30dp"
android:layout_weight = "1"
android:gravity = "center"
android:orientation = "vertical">
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id = "@+id/select_difficulty_1"
android:layout_width = "match_parent"
android:layout_height = "30dp"
android:layout_weight = "1" />
<TextView
android:id = "@+id/time_difficulty_1"
android:layout_width = "match_parent"
android:layout_height = "35dp"
android:layout_marginTop = "-40dp"
android:layout_marginBottom = "18dp"
android:background = "@drawable/time_border"
android:textSize = "17sp" />
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id = "@+id/select_difficulty_4"
android:layout_width = "match_parent"
android:layout_height = "30dp"
android:layout_weight = "1" />
<TextView
android:id = "@+id/time_difficulty_4"
android:layout_width = "match_parent"
android:layout_height = "35dp"
android:layout_marginTop = "-40dp"
android:background = "@drawable/time_border"
android:textSize = "17sp" />
</LinearLayout>
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_marginLeft = "@dimen/difficulty_margin"
android:layout_marginTop = "30dp"
android:layout_marginRight = "@dimen/difficulty_margin"
android:layout_marginBottom = "30dp"
android:layout_weight = "1"
android:gravity = "center"
android:orientation = "vertical">
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id = "@+id/select_difficulty_2"
android:layout_width = "match_parent"
android:layout_height = "30dp"
android:layout_weight = "1" />
<TextView
android:id = "@+id/time_difficulty_2"
android:layout_width = "match_parent"
android:layout_height = "35dp"
android:layout_marginTop = "-40dp"
android:layout_marginBottom = "18dp"
android:background = "@drawable/time_border"
android:textSize = "17sp" />
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id = "@+id/select_difficulty_5"
android:layout_width = "match_parent"
android:layout_height = "30dp"
android:layout_weight = "1" />
<TextView
android:id = "@+id/time_difficulty_5"
android:layout_width = "match_parent"
android:layout_height = "35dp"
android:layout_marginTop = "-40dp"
android:background = "@drawable/time_border"
android:textSize = "17sp" />
</LinearLayout>
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_marginLeft = "@dimen/difficulty_margin"
android:layout_marginTop = "30dp"
android:layout_marginRight = "@dimen/difficulty_margin"
android:layout_marginBottom = "30dp"
android:layout_weight = "1"
android:gravity = "center"
android:orientation = "vertical">
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id = "@+id/select_difficulty_3"
android:layout_width = "match_parent"
android:layout_height = "30dp"
android:layout_weight = "1" />
<TextView
android:id = "@+id/time_difficulty_3"
android:layout_width = "match_parent"
android:layout_height = "35dp"
android:layout_marginTop = "-40dp"
android:layout_marginBottom = "18dp"
android:background = "@drawable/time_border"
android:textSize = "17sp" />
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id = "@+id/select_difficulty_6"
android:layout_width = "match_parent"
android:layout_height = "30dp"
android:layout_weight = "1" />
<TextView
android:id = "@+id/time_difficulty_6"
android:layout_width = "match_parent"
android:layout_height = "35dp"
android:layout_marginTop = "-40dp"
android:background = "@drawable/time_border"
android:textSize = "17sp" />
</LinearLayout>
</LinearLayout>
<com.google.android.gms.ads.AdView xmlns:ads = "http://schemas.android.com/apk/res-auto"
android:id = "@+id/adView"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_alignParentLeft = "true"
android:layout_alignParentBottom = "true"
android:layout_marginTop = "@dimen/margintoptendp"
ads:adSize = "BANNER"
ads:adUnitId = "@string/banner_adUnitId" />
</RelativeLayout>
Добавьте xmlns:ads = "http://schemas.android.com/apk/res-auto" в корневой макет
Во фрагменте:
private AdView mBannerAd;
mBannerAd = view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
mBannerAd.loadAd(adRequest);
Добавить зависимости
implementation 'com.google.android.gms:play-services-ads:11.6.0'
Я заменил свою часть admob на фрагмент. Также я тестировал admob в Interstitial Ad с этим кодом.
//For add of Admob app ID
MobileAds.initialize(getContext(),
"ca-app-pub-3940256099942544~3347511713");
mInterstitialAd = new InterstitialAd(getContext());
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
final AdRequest request = new AdRequest.Builder().build();
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
mInterstitialAd.show();
}
@Override
public void onAdClosed() {
super.onAdClosed();
// mInterstitialAd.loadAd(request.build());
}
});
mInterstitialAd.loadAd( request );
@Waheed, это общий ответ, моя проблема не общая. Может быть нужно что-то изменить в моем макете или других частях