0
我想在我的RecyclerView中的預定位置顯示原生廣告,我已經設置了用於展示廣告模板的代碼,但是一旦我整合了用於展示Facebook Audience原生廣告的代碼, ,我不知道該怎麼做,有沒有解決方案?Java NullpointerException在Facebook受衆原生廣告中,Recyclerview
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0:
return new ViewHolderBoxOffice(layoutInflater.from(parent.getContext()).inflate(R.layout.custom_movie_box_office, parent, false));
case 2:
return new NativeAdViewHolder(layoutInflater.from(parent.getContext()).inflate(R.layout.custom_ads_facebook, parent, false));
}
/*View view=null;
if(viewType==10){
adView= (LinearLayout) layoutInflater.inflate(R.layout.custom_ads_facebook, nativeAdContainer, false);
nativeAdContainer.addView(adView);
} else {
view = layoutInflater.inflate(R.layout.custom_movie_box_office, parent, false);
}
ViewHolderBoxOffice viewHolder = new ViewHolderBoxOffice(view);
return viewHolder;*/
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case 0:
ViewHolderBoxOffice v_holder = (ViewHolderBoxOffice) holder;
Movie currentMovie = mlistMovies.get(position);
v_holder.movieTitle.setText(currentMovie.getTitle());
Date movieReleaseDate = currentMovie.getReleaseDateTheater();
if (movieReleaseDate != null) {
String formmattedDate = dateFormat.format(movieReleaseDate);
v_holder.movieReleaseDate.setText(formmattedDate);
} else {
v_holder.movieReleaseDate.setText(Constants.NA);
}
int audienceScore = currentMovie.getAudienceScore();
if (audienceScore == -1) {
v_holder.movieAudienceScore.setRating(0.0F);
v_holder.movieAudienceScore.setAlpha(0.5F);
} else {
v_holder.movieAudienceScore.setRating(currentMovie.getAudienceScore()/20.0F);
v_holder.movieAudienceScore.setAlpha(1.0F);
}
String urlThumbnail = currentMovie.getUrlThumbnail();
loadImages(urlThumbnail, v_holder);
case 2:
NativeAdViewHolder mHolder=(NativeAdViewHolder)holder;
// Setting the Text.
nativeAd.unregisterView();
mHolder.nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
mHolder.nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
mHolder.nativeAdTitle.setText(nativeAd.getAdTitle());
mHolder.nativeAdBody.setText(nativeAd.getAdBody());
// Downloading and setting the ad icon.
NativeAd.Image adIcon = nativeAd.getAdIcon();
NativeAd.downloadAndDisplayImage(adIcon, mHolder.nativeAdIcon);
// Download and setting the cover image.
NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
mHolder.nativeAdMedia.setNativeAd(nativeAd);
// Add adChoices icon
if (adChoicesView == null)
if(! (adView.getChildAt(0) instanceof AdChoicesView)){
adChoicesView = new AdChoicesView(layoutInflater.getContext(), nativeAd, true);
adView.addView(adChoicesView, 0);
}
nativeAd.registerViewForInteraction(adView);
default:
break;
}
if (position > previousPosition) {
comeagain.materialdesign.anim.AnimationUtils.animate(holder, true);
} else {
comeagain.materialdesign.anim.AnimationUtils.animate(holder, false);
}
previousPosition = position;
}
/* @Override
public void onBindViewHolder(final ViewHolderBoxOffice holder, int position) {
if (position == 10) {
} else {
}
if (position > previousPosition) {
comeagain.materialdesign.anim.AnimationUtils.animate(holder, true);
} else {
comeagain.materialdesign.anim.AnimationUtils.animate(holder, false);
}
previousPosition = position;
}*/
private void loadImages(String urlThumbnail, final ViewHolderBoxOffice holder) {
if (urlThumbnail != null) {
imageLoader.get(urlThumbnail, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.movieThumbnail.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
@Override
public int getItemCount() {
return mlistMovies.size();
}
@Override
public void onError(Ad ad, AdError adError) {
}
@Override
public void onAdLoaded(Ad ad) {
View adView = NativeAdView.render(layoutInflater.getContext(), nativeAd, NativeAdView.Type.HEIGHT_100);
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public int getItemViewType(int position) {
int viewType = 0;
if (position % 10 == 0) viewType = 2;
return viewType;
}
@Override
public void onAdsLoaded() {
}
@Override
public void onAdError(AdError adError) {
}
以下是錯誤
01-30 20:36:10.721 21295-21295/comeagain.materialdesign E/AndroidRuntime: FATAL EXCEPTION: main
Process: comeagain.materialdesign, PID: 21295
java.lang.NullPointerException
at comeagain.materialdesign.adapters.AdapterBoxOffice.onBindViewHolder(AdapterBoxOffice.java:165)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
我收到錯誤的改變到該行
if(! (adView.getChildAt(0) instanceof AdChoicesView)){
,但仍然得到NullPointerException異常
if(! (mHolder.adview.getChildAt(0) instanceof AdChoicesView)){
AdChoicesView adChoicesView = new AdChoicesView(layoutInflater.getContext(), nativeAd, true);
mHolder.adview.addView(adChoicesView, 0);
}
nativeAd.registerViewForInteraction(mHolder.adview);
default:
break;
}
if (position > previousPosition) {
comeagain.materialdesign.anim.AnimationUtils.animate(holder, true);
} else {
comeagain.materialdesign.anim.AnimationUtils.animate(holder, false);
}
previousPosition = position;
}
/* @Override
public void onBindViewHolder(final ViewHolderBoxOffice holder, int position) {
if (position == 10) {
} else {
}
if (position > previousPosition) {
comeagain.materialdesign.anim.AnimationUtils.animate(holder, true);
} else {
comeagain.materialdesign.anim.AnimationUtils.animate(holder, false);
}
previousPosition = position;
}*/
private void loadImages(String urlThumbnail, final ViewHolderBoxOffice holder) {
if (urlThumbnail != null) {
imageLoader.get(urlThumbnail, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.movieThumbnail.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
@Override
public int getItemCount() {
return mlistMovies.size();
}
@Override
public void onError(Ad ad, AdError adError) {
}
@Override
public void onAdLoaded(Ad ad) {
}
@Override
public void onAdClicked(Ad ad) {
L.m("Ads is loaded, Jeez man that is good");
System.out.println("Loaded in fragment");
nativeAd = manager.nextNativeAd();
nativeAd.setAdListener(this);
}
@Override
public int getItemViewType(int position) {
int viewType = 0;
if (position % 10 == 0) viewType = 2;
return viewType;
}
@Override
public void onAdsLoaded() {
L.m("Ads is loaded, Jeez man that is good");
System.out.println("Loaded in fragment");
nativeAd = manager.nextNativeAd();
nativeAd.setAdListener(this);
}
@Override
public void onAdError(AdError adError) {
}
class NativeAdViewHolder extends RecyclerView.ViewHolder {
private ImageView nativeAdIcon;
private TextView nativeAdTitle;
private TextView nativeAdBody;
private MediaView nativeAdMedia;
private TextView nativeAdSocialContext;
private Button nativeAdCallToAction;
private LinearLayout adview;
public NativeAdViewHolder(View itemView) {
super(itemView);
// Create native UI using the ad metadata.
nativeAdIcon = (ImageView) itemView.findViewById(R.id.native_ad_icon);
nativeAdTitle = (TextView) itemView.findViewById(R.id.native_ad_title);
nativeAdBody = (TextView) itemView.findViewById(R.id.native_ad_body);
nativeAdMedia = (MediaView) itemView.findViewById(R.id.native_ad_media);
nativeAdSocialContext = (TextView) itemView.findViewById(R.id.native_ad_social_context);
nativeAdCallToAction = (Button) itemView.findViewById(R.id.native_ad_call_to_action);
adview=(LinearLayout) itemView.findViewById(R.id.ads_unit);
}
}
以下是錯誤
01-30 23:13:06.031 14318-14318/comeagain.materialdesign E/AndroidRuntime: FATAL EXCEPTION: main
Process: comeagain.materialdesign, PID: 14318
java.lang.NullPointerException
at com.facebook.ads.AdChoicesView.a(Unknown Source)
at com.facebook.ads.AdChoicesView.<init>(Unknown Source)
at comeagain.materialdesign.adapters.AdapterBoxOffice.onBindViewHolder(AdapterBoxOffice.java:171)
錯誤發生在這個點
AdChoicesView adChoicesView = new AdChoicesView(layoutInflater.getContext(), nativeAd, true);
您在第165行獲得NullPointer錯誤。哪一行? –