- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过将它们作为替代 View 包含在我的 RecyclerAdapter 中来实现 Facebook 原生广告。有没有人有这方面的经验?为了更好地衡量,我已经包括了我的整个类(class)。
public class FeedRecyclerAdapter extends LoadingRowRecyclerAdapter implements AdListener {
private static final int VIEW_TYPE_FEED = 1;
private static final int VIEW_TYPE_AD = 2;
private final Context context;
private final List<Feed> feedItems;
private final ImageLoader feedItemImageLoader;
private FeedItemClickListener feedItemClickListener;
private boolean isLongPressed = false;
private NativeAd nativeAd;
private NativeAdsManager manager;
View adView;
public FeedRecyclerAdapter(Context context, List<Feed> feedItems, ImageLoader feedItemImageLoader, NativeAd nativeAd, NativeAdsManager manager) {
this.context = context;
this.feedItems = feedItems;
this.feedItemImageLoader = feedItemImageLoader;
this.nativeAd = nativeAd;
this.manager = manager;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case VIEW_TYPE_FEED:
return new FeedViewHolder(new FeedItemView(context));
case VIEW_TYPE_AD:
return new AdditionalHolder(LayoutInflater.from(context).inflate(R.layout.ad_test3, parent, false));
}
return super.onCreateViewHolder(parent, viewType);
}
public class AdditionalHolder extends RecyclerView.ViewHolder {
protected LinearLayout templateContainer;
protected ImageView blank_holder;
private LinearLayout nativeAdContainer;
private LinearLayout adView;
private AdChoicesView adChoicesView;
ImageView nativeAdIcon;
TextView nativeAdTitle;
TextView nativeAdBody;
MediaView nativeAdMedia;
TextView nativeAdSocialContext;
Button nativeAdCallToAction;
public AdditionalHolder(View view) {
super(view);
//// Add ad into the ad container.
nativeAdContainer = (LinearLayout) view.findViewById(R.id.ad_unit);
LayoutInflater inflater = LayoutInflater.from(context);
adView = (LinearLayout) inflater.inflate(R.layout.ad_test3, nativeAdContainer, false);
nativeAdContainer.addView(adView);
// Create native UI using the ad metadata.
nativeAdIcon = (ImageView) view.findViewById(R.id.native_ad_icon);
nativeAdTitle = (TextView) view.findViewById(R.id.native_ad_title);
nativeAdBody = (TextView) view.findViewById(R.id.native_ad_body);
nativeAdMedia = (MediaView) view.findViewById(R.id.native_ad_media);
nativeAdSocialContext = (TextView) view.findViewById(R.id.native_ad_social_context);
nativeAdCallToAction = (Button) view.findViewById(R.id.native_ad_call_to_action);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
if (getItemViewType(position) == VIEW_TYPE_FEED) {
bindFeedItemView((FeedViewHolder) viewHolder, position);
} else if (getItemViewType(position) == VIEW_TYPE_AD) {
AdditionalHolder new_holder = (AdditionalHolder) viewHolder;
try {
new_holder.templateContainer.removeViewInLayout(adView);
} catch (Exception e) {
e.printStackTrace();
}
nativeAd = manager.nextNativeAd();
try {
adView = NativeAdView.render(context, nativeAd, NativeAdView.Type.HEIGHT_300);
} catch (NullPointerException e) {
e.printStackTrace();
}
new_holder.templateContainer.addView(adView);
new_holder.blank_holder.setVisibility(View.GONE);
// Setting the Text.
new_holder.nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
new_holder.nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
new_holder.nativeAdTitle.setText(nativeAd.getAdTitle());
new_holder.nativeAdBody.setText(nativeAd.getAdBody());
// Downloading and setting the ad icon.
NativeAd.Image adIcon = nativeAd.getAdIcon();
NativeAd.downloadAndDisplayImage(adIcon, new_holder.nativeAdIcon);
// Download and setting the cover image.
NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
new_holder.nativeAdMedia.setNativeAd(nativeAd);
// Add adChoices icon
if (new_holder.adChoicesView == null) {
new_holder.adChoicesView = new AdChoicesView(context, nativeAd, true);
new_holder.adView.addView(new_holder.adChoicesView, 0);
}
nativeAd.registerViewForInteraction(new_holder.adView);
}
}
// Differentiate between feedItem views and nativeAds
@Override
public int getItemViewType(int position) {
int viewType = 1;
if ((position % 9 == 0) && position != 1) viewType = 2;
return viewType;
}
// For feedItems
private void bindFeedItemView(FeedViewHolder viewHolder, int position) {
final FeedItemView feedItemView = viewHolder.feedItemView;
final Feed feedDesign = feedItems.get(position);
// TODO we can probably conditionally show or hide these based on the type of feed item, same as in FeedActivitySingle
feedItemView.showOrHideEditButton(true);
feedItemView.showOrHideBuyButton(false);
feedItemView.showOrHideFlipButton(false);
feedItemView.showOrHidePriceText(false);
// Set the results into TextViews
feedItemView.setProductPriceText(String.valueOf(feedDesign.getDesign().getPrice()));
feedItemView.setDownloadsText(String.valueOf(feedDesign.getDesign().getDownloadCount()));
feedItemView.setLikesText(String.valueOf(feedDesign.getDesign().getLikesCount()));
feedItemView.setUsernameText(feedDesign.getDesign().getAuthor().getUsername());
feedItemView.setTimestampText(feedDesign.getTimestampText());
feedItemView.getSaveImage().setImageResource(feedDesign.isInPersonalGallery() ? R.drawable.ic_action_saved : R.drawable.ic_not_saved);
feedItemView.getLikeImage().setImageResource(feedDesign.isLiked() ? R.drawable.ic_action_like_feed_full : R.drawable.ic_action_like_feed);
feedItemView.getTrashImage().setVisibility(ParseHelper.isCurrentUser(feedDesign.getDesign().getAuthor().getObjectId()) ? View.VISIBLE : View.GONE);
feedItemView.getFeedSocialShareImage().setVisibility(View.VISIBLE);
feedItemImageLoader.DisplayImage(feedDesign.getDesign().getCompressedImage().getUrl(), feedItemView.getImage(), feedItemView.getProgressBar());
if(feedDesign.getDesign().getAuthor().getProfilePicture() != null) {
feedItemImageLoader.DisplayImage(feedDesign.getDesign().getAuthor().getProfilePicture().getUrl(), feedItemView.getProfilePicture(), null); // TODO should this use profilePictureFileCache?
} else {
viewHolder.feedItemView.getProfilePicture().setImageResource(R.drawable.ic_anonymous);
}
SetCommentViews(feedItemView, feedDesign.getComments());
SetClickListeners(feedItemView, feedDesign);
}
@Override
protected int getContentDataSize() {
return feedItems.size();
}
@Override
protected int getViewType(int position) {
if (position == 1) {
return VIEW_TYPE_FEED;
} else {
return VIEW_TYPE_AD;
}
}
// Facebook Ad methods
@Override
public void onError(Ad ad, AdError adError) {
}
@Override
public void onAdLoaded(Ad ad) {
}
@Override
public void onAdClicked(Ad ad) {
}
public void setFeedItemClickListener(FeedItemClickListener feedItemClickListener) {
this.feedItemClickListener = feedItemClickListener;
}
private void SetCommentViews(FeedItemView feedItemView, List<Comment> comments) {
feedItemView.getViewAllComments().setVisibility(View.VISIBLE);
feedItemView.setViewAllComments("View more comments");
// reset layouts to hidden
feedItemView.getComment1Layout().setVisibility(View.GONE);
feedItemView.getComment2Layout().setVisibility(View.GONE);
feedItemView.getComment3Layout().setVisibility(View.GONE);
if (comments == null || comments.size() == 0) {
feedItemView.getViewAllComments().setVisibility(View.GONE);
return;
}
// note these are meant to fall-through without break statements
switch (comments.size()) {
case 3:
feedItemView.getComment3Layout().setVisibility(View.VISIBLE);
feedItemView.setCommentUser3(comments.get(2).getAuthor().getUsername());
feedItemView.setCommentText3(comments.get(2).getComment());
case 2:
feedItemView.getComment2Layout().setVisibility(View.VISIBLE);
feedItemView.setCommentUser2(comments.get(1).getAuthor().getUsername());
feedItemView.setCommentText2(comments.get(1).getComment());
case 1:
feedItemView.getComment1Layout().setVisibility(View.VISIBLE);
feedItemView.setCommentUser1(comments.get(0).getAuthor().getUsername());
feedItemView.setCommentText1(comments.get(0).getComment());
break;
}
}
private void SetClickListeners(final FeedItemView feedItemView, final Feed feedDesign) {
// setup gesture detector with current view, view holder, and feed design
final GestureDetector gestureDetector = new GestureDetector(context, new GestureListener(feedDesign, feedItemView));
// set gesture detector on image view
feedItemView.setImageTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == ACTION_UP){
if (isLongPressed){
isLongPressed = false;
feedItemClickListener.onMarkteFeedImageLongPressed_Release();
Log.d("anyTAG","Up");
return true;
}
}
if (motionEvent.getAction() == ACTION_MOVE){
return true;
}
return gestureDetector.onTouchEvent(motionEvent);
}
});
feedItemView.setTrashImageClickListener(v -> {
if (feedItemClickListener != null) {
feedItemClickListener.onTrashClicked(feedDesign);
}
});
feedItemView.setFeedSocialShareClickListener(v -> {
if (feedItemClickListener != null) {
try {
feedItemClickListener.onSocialShareClicked(feedDesign);
} catch (IOException e) {
e.printStackTrace();
}
}
});
feedItemView.setLikeImageClickListener(v -> {
if (feedItemClickListener != null) {
feedItemClickListener.onLikeClicked(feedDesign, feedItemView);
}
});
feedItemView.setSaveImageClickListener(v -> {
if (feedItemClickListener != null) {
feedItemClickListener.onSaveClicked(feedDesign, feedItemView);
}
});
feedItemView.setAuthorClickListener(v -> {
if (feedItemClickListener != null) {
feedItemClickListener.onAuthorClicked(feedDesign);
}
});
feedItemView.setCommentTextClickListener(v -> {
if (feedItemClickListener != null) {
feedItemClickListener.onCreateCommentClicked(feedDesign);
}
});
feedItemView.setViewAllCommentsClickListener(v -> {
if (feedItemClickListener != null) {
feedItemClickListener.onCreateCommentClicked(feedDesign);
}
});
feedItemView.setEditImageClickListener(v -> {
if (feedItemClickListener != null) {
feedItemClickListener.onCreateEditImageClicked(feedDesign);
}
});
feedItemView.setCommentImageClickListener(v -> {
if (feedItemClickListener != null) {
feedItemClickListener.onCreateCommentClicked(feedDesign);
}
});
feedItemView.setCommentUser1ClickListener(v -> {
if (feedItemClickListener != null) {
feedItemClickListener.onAuthorClicked(feedDesign.getComments().get(0).getAuthor().getObjectId());
}
});
feedItemView.setCommentUser2ClickListener(v -> {
if (feedItemClickListener != null) {
feedItemClickListener.onAuthorClicked(feedDesign.getComments().get(1).getAuthor().getObjectId());
}
});
feedItemView.setCommentUser3ClickListener(v -> {
if (feedItemClickListener != null) {
feedItemClickListener.onAuthorClicked(feedDesign.getComments().get(2).getAuthor().getObjectId());
}
});
feedItemView.setProfilePictureClickListener(v -> {
if (feedItemClickListener != null) {
feedItemClickListener.onProfilePictureClicked(feedDesign);
}
});
}
class FeedViewHolder extends RecyclerView.ViewHolder {
FeedItemView feedItemView;
public FeedViewHolder(FeedItemView view) {
super(view);
this.feedItemView = view;
}
}
// Gesture detector for double tap
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
private Feed feedDesign;
private FeedItemView feedItemView;
public GestureListener(Feed feedDesign, FeedItemView feedItemView) {
this.feedDesign = feedDesign;
this.feedItemView = feedItemView;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onDoubleTap(MotionEvent event) {
Log.d("FeedRecyclerAdapter", feedDesign.getDesign().getCompressedImage().getUrl());
if (feedItemClickListener != null) {
feedItemClickListener.onImageDoubleTapped(feedDesign, feedItemView);
}
return true;
}
@Override
public void onLongPress(MotionEvent event) {
if (event.getAction() == ACTION_DOWN)
{
if (!isLongPressed){
isLongPressed = true;
feedItemClickListener.onMarketFeedImageLongPressed(feedDesign, feedDesign.getDisplayedSide());
}
}
}
}
}
我收到以下异常:
07-01 16:32:12.509 23168-23168/com.elgami.customizer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.elgami.customizer, PID: 23168
java.lang.NullPointerException: Attempt to invoke virtual method 'com.facebook.ads.NativeAd com.facebook.ads.NativeAdsManager.nextNativeAd()' on a null object reference
at com.elgami.feed.FeedRecyclerAdapter.onBindViewHolder(FeedRecyclerAdapter.java:121)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5037)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:598)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Ha
首先,它似乎在提示这一行:
nativeAd = manager.nextNativeAd();
这是我的 FeedActivity 类的要点:
https://gist.github.com/anonymous/bd1b6afd21d27744a99970d3f51aa8a0
最佳答案
NullPointerException
当您尝试调用方法或访问对象引用的字段但该引用不包含有效对象时发生。一个简单的例子是:
String someString;
char firstChar = someString.charAt(0); // ERROR!
此代码会引发错误,因为在调用 charAt
时,someString
变量未引用有效的 String
实例。请注意,第一行没有创建 String
实例,它仅声明了一个可能指向 String
的变量>。事实上,这段代码等同于:
String someString = null;
char firstChar = someString.charAt(0);
有关 NullPointerException
的更多信息,它们发生的原因以及如何修复它们:see this question .
在您的情况下,错误是由 line 176 in FeedActivity
引起的:
feedRecyclerAdapter = new FeedRecyclerAdapter(this, feedItems, new ImageLoader(new FeedItemFileCache(this)), nativeAd, manager);
这里,manager
是FeedActivity
类的实例字段,定义在line 87 上。 :
public NativeAdsManager manager;
问题是这个字段从来没有被赋值:这个字段没有赋值(比如manager = ...
)。因此,当执行第 176 行时,FeedRecyclerAdapter
接收 null
作为它的 manager
参数,并抛出一个 NullPointerException
稍后在 onBindViewHolder
中尝试使用该 manager
时。
解决方案是在您的 FeedActivity
中创建一个有效的 NativeAdsManager
并将其分配给 manager
,然后再传递给 FeedRecyclerAdapter
。 From the Facebook documentation about NativeAdsManager
,这样的代码应该是这样的:
public class YourActivity extends Activity implements NativeAdsManager.Listener {
private NativeAdsManager manager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
manager = new NativeAdsManager(this, "YOUR_PLACEMENT_ID", 5);
manager.setListener(this);
manager.loadAds();
}
}
关于java - 使用 RecyclerAdapter 的 Facebook 原生广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38162205/
我在网上搜索但没有找到任何合适的文章解释如何使用 javascript 使用 WCF 服务,尤其是 WebScriptEndpoint。 任何人都可以对此给出任何指导吗? 谢谢 最佳答案 这是一篇关于
我正在编写一个将运行 Linux 命令的 C 程序,例如: cat/etc/passwd | grep 列表 |剪切-c 1-5 我没有任何结果 *这里 parent 等待第一个 child (chi
所以我正在尝试处理文件上传,然后将该文件作为二进制文件存储到数据库中。在我存储它之后,我尝试在给定的 URL 上提供文件。我似乎找不到适合这里的方法。我需要使用数据库,因为我使用 Google 应用引
我正在尝试制作一个宏,将下面的公式添加到单元格中,然后将其拖到整个列中并在 H 列中复制相同的公式 我想在 F 和 H 列中输入公式的数据 Range("F1").formula = "=IF(ISE
问题类似于this one ,但我想使用 OperatorPrecedenceParser 解析带有函数应用程序的表达式在 FParsec . 这是我的 AST: type Expression =
我想通过使用 sequelize 和 node.js 将这个查询更改为代码取决于在哪里 select COUNT(gender) as genderCount from customers where
我正在使用GNU bash,版本5.0.3(1)-发行版(x86_64-pc-linux-gnu),我想知道为什么简单的赋值语句会出现语法错误: #/bin/bash var1=/tmp
这里,为什么我的代码在 IE 中不起作用。我的代码适用于所有浏览器。没有问题。但是当我在 IE 上运行我的项目时,它发现错误。 而且我的 jquery 类和 insertadjacentHTMl 也不
我正在尝试更改标签的innerHTML。我无权访问该表单,因此无法编辑 HTML。标签具有的唯一标识符是“for”属性。 这是输入和标签的结构:
我有一个页面,我可以在其中返回用户帖子,可以使用一些 jquery 代码对这些帖子进行即时评论,在发布新评论后,我在帖子下插入新评论以及删除 按钮。问题是 Delete 按钮在新插入的元素上不起作用,
我有一个大约有 20 列的“管道分隔”文件。我只想使用 sha1sum 散列第一列,它是一个数字,如帐号,并按原样返回其余列。 使用 awk 或 sed 执行此操作的最佳方法是什么? Accounti
我需要将以下内容插入到我的表中...我的用户表有五列 id、用户名、密码、名称、条目。 (我还没有提交任何东西到条目中,我稍后会使用 php 来做)但由于某种原因我不断收到这个错误:#1054 - U
所以我试图有一个输入字段,我可以在其中输入任何字符,但然后将输入的值小写,删除任何非字母数字字符,留下“。”而不是空格。 例如,如果我输入: 地球的 70% 是水,-!*#$^^ & 30% 土地 输
我正在尝试做一些我认为非常简单的事情,但出于某种原因我没有得到想要的结果?我是 javascript 的新手,但对 java 有经验,所以我相信我没有使用某种正确的规则。 这是一个获取输入值、检查选择
我想使用 angularjs 从 mysql 数据库加载数据。 这就是应用程序的工作原理;用户登录,他们的用户名存储在 cookie 中。该用户名显示在主页上 我想获取这个值并通过 angularjs
我正在使用 autoLayout,我想在 UITableViewCell 上放置一个 UIlabel,它应该始终位于单元格的右侧和右侧的中心。 这就是我想要实现的目标 所以在这里你可以看到我正在谈论的
我需要与 MySql 等效的 elasticsearch 查询。我的 sql 查询: SELECT DISTINCT t.product_id AS id FROM tbl_sup_price t
我正在实现代码以使用 JSON。 func setup() { if let flickrURL = NSURL(string: "https://api.flickr.com/
我尝试使用for循环声明变量,然后测试cols和rols是否相同。如果是,它将运行递归函数。但是,我在 javascript 中执行 do 时遇到问题。有人可以帮忙吗? 现在,在比较 col.1 和
我举了一个我正在处理的问题的简短示例。 HTML代码: 1 2 3 CSS 代码: .BB a:hover{ color: #000; } .BB > li:after {
我是一名优秀的程序员,十分优秀!