@Override
public void onScreenLoad() {
super.onScreenLoad();
loadNativeAd();
}
private void loadNativeAd() {
// Instantiate a NativeAd object.
// NOTE: the placement ID will eventually identify this as your App, you can ignore it for
// now, while you are testing and replace it later when you have signed up.
// While you are using this temporary code you will only get test ads and if you release
// your code like this to the Google Play your users will not receive ads (you will get a no fill error).
nativeAd = new NativeAd(getContext(), "YOUR_PLACEMENT_ID");
nativeAd.setAdListener(new NativeAdListener() {
@Override
public void onMediaDownloaded(Ad ad) {
// Native ad finished downloading all assets
Log.e(TAG1, "Native ad finished downloading all assets.");
}
@Override
public void onError(Ad ad, AdError adError) {
// Native ad failed to load
Log.e(TAG1, "Native ad failed to load: " + adError.getErrorMessage());
}
@Override
public void onAdLoaded(Ad ad) {
// Native ad is loaded and ready to be displayed
Log.d(TAG1, "Native ad is loaded and ready to be displayed!");
// Race condition, load() called again before last ad was displayed
if (nativeAd == null || nativeAd != ad) {
return;
}
if (nativeAd != null) {
// Inflate Native Ad into Container
inflateAd(nativeAd);
// Registering a touch listener to log which ad component receives the touch event.
// We always return false from onTouch so that we don't swallow the touch event (which
// would prevent click events from reaching the NativeAd control).
// The touch listener could be used to do animations.
nativeAd.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int i = view.getId();
if (i == R.id.native_ad_call_to_action) {
Log.d(TAG, "Call to action button clicked");
} else if (i == R.id.native_ad_media) {
Log.d(TAG, "Main image clicked");
} else {
Log.d(TAG, "Other ad component clicked");
}
}
return false;
}
});
}
}
@Override
public void onAdClicked(Ad ad) {
// Native ad clicked
Log.d(TAG1, "Native ad clicked!");
if (!(getStringValueFromType(ConfigTags.SOURCE_TYPE.SESSION, AppConstants.SES_ADVIEW).equalsIgnoreCase(getStringValueFromType(ConfigTags.SOURCE_TYPE.DEVICE, "DEVICE_CURRENT_DATE")))) {
//Save session permanent Action
removeSession(AppConstants.SES_ADVIEW);
//Save session permanent Action
CITCoreActivity.saveSessionValue(getCitCoreActivity(), AppConstants.SES_ADVIEW, getStringValueFromType(ConfigTags.SOURCE_TYPE.DEVICE, "DEVICE_CURRENT_DATE"), false);
}
}
@Override
public void onLoggingImpression(Ad ad) {
// Native ad impression
Log.d(TAG1, "Native ad impression logged!");
}
});
// Request an ad
nativeAd.loadAd();
}
private void inflateAd(NativeAd nativeAd) {
nativeAd.unregisterView();
// Add the Ad view into the ad container.
nativeAdLayout = mainView.findViewById(R.id.native_ad_container);
LayoutInflater inflater1 = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Inflate the Ad view. The layout referenced should be the one you created in the last step.
adView = (LinearLayout) inflater1.inflate(R.layout.native_ad_layout, nativeAdLayout, false);
nativeAdLayout.addView(adView);
// Add the AdOptionsView
LinearLayout adChoicesContainer = mainView.findViewById(R.id.ad_choices_container);
AdOptionsView adOptionsView = new AdOptionsView(getActivity(), nativeAd, nativeAdLayout);
adChoicesContainer.removeAllViews();
adChoicesContainer.addView(adOptionsView, 0);
// Create native UI using the ad metadata.
MediaView nativeAdIcon = adView.findViewById(R.id.native_ad_icon);
TextView nativeAdTitle = adView.findViewById(R.id.native_ad_title);
MediaView nativeAdMedia = adView.findViewById(R.id.native_ad_media);
TextView nativeAdSocialContext = adView.findViewById(R.id.native_ad_social_context);
TextView nativeAdBody = adView.findViewById(R.id.native_ad_body);
TextView sponsoredLabel = adView.findViewById(R.id.native_ad_sponsored_label);
Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);
// Set the Text.
nativeAdTitle.setText(nativeAd.getAdvertiserName());
nativeAdBody.setText(nativeAd.getAdBodyText());
nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);
nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
sponsoredLabel.setText(nativeAd.getSponsoredTranslation());
// Create a list of clickable views
List<View> clickableViews = new ArrayList<>();
//clickableViews.add(nativeAdTitle);
clickableViews.add(nativeAdCallToAction);
// Register the Title and CTA button to listen for clicks.
nativeAd.registerViewForInteraction(
adView,
nativeAdMedia,
nativeAdIcon,
clickableViews);
}
Приложение вылетает в месте, которое предлагает загрузку рекламы. Код всего раздела указан выше. Какие изменения необходимо сделать, чтобы предотвратить появление в приложении cra sh из-за вышеприведенного кода? Возникла ошибка:
java.lang.NullPointerException
Может кто-нибудь помочь? Как вы думаете, что можно сделать с помощью приведенного выше кода?
Заранее спасибо.