Я пытаюсь добавить ViewGroup в линейный макет после добавления этого в макет предыдущего действия, но получаю исключение IllegalStateException.
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Первое действие:
public class TestActivity extends AppCompatActivity implements AdMobAdManager.InitialCallBack {
LinearLayout layoutAdView;
Button btnNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
AdMobAdManager.getInstance(getApplicationContext());
AdMobAdManager.getInstance(getApplicationContext()).initialCallBack(this);
btnNext = findViewById(R.id.btnNext);
btnNext.setEnabled(false);
layoutAdView = findViewById(R.id.layoutAdView);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(TestActivity.this,TestActivity2.class));
}
});
}
@Override
public void onInit() {
btnNext.setEnabled(true);
ViewGroup viewGroup = AdMobAdManager.getInstance(getApplicationContext()).loadBannerAd();
layoutAdView.addView(viewGroup);
}
}
Здесь XML TestActivity:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestActivity">
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="Next"
android:id="@+id/btnNext"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layoutAdView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Вот код второго действия:
public class TestActivity2 extends AppCompatActivity {
LinearLayout layoutAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2);
layoutAdView = findViewById(R.id.layoutAdView);
addBanner();
}
private void addBanner() {
ViewGroup viewGroup = AdMobAdManager.getInstance(getApplicationContext()).loadBannerAd();
layoutAdView.addView(viewGroup);
}
}
Вот XML второго занятия:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layoutAdView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
В общем, я загружаю рекламный баннер Admob в первое действие и пытаюсь добавить этот баннер во второе действие.
Вот код запроса на загрузку баннера:
public class AdMobAdManager {
private static AdMobAdManager ourInstance;
private Context context;
private AdView bannerAdView;
private boolean isInitialised, isInitialising;
private InitialCallBack initialCallBack;
public synchronized static AdMobAdManager getInstance(Context context) {
if (ourInstance == null) {
ourInstance = new AdMobAdManager(context);
}
return ourInstance;
}
private AdMobAdManager(Context context){
this.context = context;
isInitialising = true;
MobileAds.initialize(context, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
Log.v("TAG_ADMOB","onInitializationComplete");
isInitialising = false;
isInitialised = true;
if (initialCallBack != null){
initialCallBack.onInit();
}
}
});
}
public void initialCallBack(InitialCallBack initialCallBack){
this.initialCallBack = initialCallBack;
if (isInitialised()){
initialCallBack.onInit();
}
}
public boolean isInitialised() {
return isInitialised;
}
public boolean isInitialising() {
return isInitialising;
}
public AdView loadBannerAd(){
if (bannerAdView == null){
bannerAdView = new AdView(context);
bannerAdView.setAdSize(AdSize.BANNER);
bannerAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
bannerAdView.setAdListener(new AdListener());
bannerAdView.loadAd(new AdRequest.Builder().build());
}else {
bannerAdView.loadAd(new AdRequest.Builder().build());
}
return bannerAdView;
}
public interface InitialCallBack{
void onInit();
}
}
Может кто-нибудь сказать мне, что не так в этом коде и как я могу решить его?