问题
I have a problem with AdSence. I have tried to resolve it the last couple of days but without any good results. I used developers guide but it still empty on the place of banner.
What am I doing in an application. First of all I have added a TableRow object and added AdView to it:
banner = new TableRow(engine);
banner.setGravity(Gravity.TOP & Gravity.RIGHT);
adView = new AdView(engine, AdSize.BANNER, "a14def8xxxxxxxx");
banner.addView(adView);
AdRequest request = new AdRequest();
adView.loadAd(request);
Afterwards I just added this "banner" object to the other view. At the end - there is no output there. In case I changed AdView to TextView, just for the proof of concept, it works fine.
The output log:
06-13 18:04:38.476: INFO/Ads(576): Received ad url: <"url": "http://r.admob.com:80/ad_source.php?... > 06-13 18:04:40.406: INFO/Ads(576): onReceiveAd()
The only strange thing for me in log is:
06-13 18:04:40.336: WARN/webcore(576): Can't get the viewWidth after the first layout
I haven't found what is it means and is it because of AdSence.
Update
I have the class:
public class QuestEngine extends Activity {
Then I am trying to produce new AdView in method:
public IntroView(QuestEngine engine) {
That is why in "new AdView" I am using engine object.
回答1:
Change the new AdView declaration to:
adView = new AdView(this, AdSize.BANNER, "a14def820df0417");
The engine object does not contain the context for its dimensions. The dimensions of the table row are in the Activity context "this"
回答2:
Well if you are setting your adView
via xml/layout file
then do as directed below:-
In your activity.xml
file insert the following tag where you want your AdView
to be shown:-
<com.google.ads.doubleclick.DfpAdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="/XXXXX/XXXXX"
ads:adSize="BANNER"
/>
And in your Activity.java
file write this code in your onCreate() Method
of the Activity Life Cycle
:-
// Look up the DfpAdView as a resource and load a request.
adView = (DfpAdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
Now if you want to programmatic set the AdView
in your Activity then follow the steps as directed below:-
Write the following code in your onCreate() method
,
I am demonstrating for Fragment
, so write the following code in your onActivityCreated()Method
.
// Create an ad view as a second header for now to meet deadlines
DfpAdView mAdView=null; //you can also define this as a Global Variable.
if (mAdView == null) {
mAdView = new DfpAdView(getActivity(), AdSize.BANNER, "/XXXX/XXXX"); //give your adId incase of XXXX
iaddHeight= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()); //This code is used to set the height of the addHeight as 50 pixels, which could be used in layout params.
RelativeLayout fl =new RelativeLayout(this.getActivity());
RelativeLayout.LayoutParams flParams =new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
flParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
FrameLayout.LayoutParams para =new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
para.setMargins(0, 0, 0,iaddHeight);
getListView().setLayoutParams(para);
this.getActivity().addContentView(fl, flParams);
fl.addView(mAdView,flParams);
}
mAdView.loadAd(new AdRequest());
Hope This can help Any Future users...
来源:https://stackoverflow.com/questions/6333076/android-adsense-cant-see-an-advertisement-in-an-application