The ad unit ID must be set on InterstitialAd before loadAd is called.이게 해결이 안되요.ㅠㅠ
조회수 463회
이건 MainActivity .java이구요.
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
//광고로드
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
//배너광고
import com.google.android.gms.ads.AdView;
//전면광고
import com.google.android.gms.ads.InterstitialAd;
public class MainActivity extends AppCompatActivity {
//배너광고
private AdView mAdView;
//전면광고
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//배너광고
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
//전면광고
String Interstitial_id = MyApplication.Interstitial_ad_unit_id;
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(Interstitial_id);
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
@Override
public void onBackPressed() {
// 혹시 웹 뷰를 사용하는 액티비티일 경우 웹뷰 뒤로가기를 구현해준다.
/* if (webView.canGoBack()) {
webView.goBack();
} else { */
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// 사용자가 광고를 닫으면 뒤로가기 이벤트를 발생시킨다.
finish();
}
});
} else {
super.onBackPressed();
}
}
}
이건 activity_main.xlm입니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</LinearLayout>
string.xml
<resources>
<string name="app_name">kidsyube</string>
<!-- 테스트용-->
<string name="APPLICATION_ID">ca-app-pub-3940256099942544~3347511713</string>
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
<string name="Interstitial_ad_unit_id">ca-app-pub-3940256099942544/1033173712</string>
</resources>
MyApplication.java
import android.app.Application;
public class MyApplication extends Application {
public static String Interstitial_ad_unit_id;
public static String APPLICATION_ID;
@Override
public void onCreate(){
super.onCreate();
//테스트용
MyApplication.Interstitial_ad_unit_id = "ca-app-pub-3940256099942544/1033173712";
MyApplication.APPLICATION_ID = "ca-app-pub-3940256099942544~3347511713";
}
}
이렇게 했는데 에러가납니다. 이거 말고 다른 앱을 만들때는 이런 일이 없었거든요. 참고로 메니페스토에도
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
이거 추가했습니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력