I am trying to test the adMob on my app. I follow the steps from https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start and everything looks good except this part:
It says on the site: "add the PlaceHolder fragment in the RelativeLayout, and include the AdFragment below the PlaceHolder fragment:"
<fragment android:name="com.google.android.gms.example.bannerexample.MyActivity$PlaceholderFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/adFragment" />
<fragment
android:id="@+id/adFragment"
android:name="com.google.android.gms.example.bannerexample.MyActivity$AdFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
when I add this part to my xml, this part below has a red color and it says "cannot resolve symbol"
.example.bannerexample.MyActivity$PlaceholderFragment
There is no explanation about that, I think I have to change some part of this line according to my app but which part?
Or somthing else that I missed?
Update:
logcat says that:
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.example.bannerexample.MyActivity$PlaceholderFragment: make sure class name exists, is public, and has an empty constructor that is public
Update 2:
I have solved this problem by changing the full line with "com.myc.myapp.myactivity$PlaceholderFragment"
nothing appears on the ad layout, just empty.
Update 3:
I added an adListener and I get the toast from onAdLoaded function. But there is nothing on the screen except my other layouts. The Layout that I used for ad is empty.
mAdView.setAdListener(new AdListener() {
@Override
public void onAdOpened() {
// Save app state before going to the ad overlay.
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
msg = errorCode +"";
Toast.makeText(c, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void onAdLoaded() {
super.onAdLoaded();
msg = "done";
Toast.makeText(c, msg, Toast.LENGTH_SHORT).show();
}
});
Update 4:
my class where I wanted to show the add:
public class OyunAna extends ActionBarActivity {
.
.
.
.
.
.
.
.
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container, false);
return rootView;
}
}
public static class AdFragment extends Fragment {
private AdView mAdView;
public AdFragment() {
}
@Override
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
// Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
// values/strings.xml.
mAdView = (AdView) getView().findViewById(R.id.adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device. e.g.
// "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("my device id goes here")
.build();
// Start loading the ad in the background.
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
@Override
public void onAdOpened() {
// Save app state before going to the ad overlay.
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
msg = errorCode +"";
Toast.makeText(c, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void onAdLoaded() {
super.onAdLoaded();
msg = "done";
Toast.makeText(c, msg, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_ad, container, false);
}
/** Called when leaving the activity */
@Override
public void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}
/** Called when returning to the activity */
@Override
public void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}
/** Called before the activity is destroyed */
@Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}
}
fragment_ad:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" />
</RelativeLayout>
my activity layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:orientation="vertical"
tools:context="com.cdev.colormatch.OyunAna"
android:background="#FFFFFF" >
<RelativeLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.1">
<fragment
android:name="com.cdev.colormatch.OyunAna$PlaceholderFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/adFragment" />
<fragment
android:id="@+id/adFragment"
android:name="com.cdev.colormatch.OyunAna$AdFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
.
.
.
.
</LinearLayout>