2

The ads show up perfectly on the emulator but I still see this error on the device preview. This error blocks the preview with a rendering problems. I've tried everything in SO and nothing worked. Here is my .java:

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;


public class ols_home extends ActionBarActivity {

    private AdView adView;
    private static final String AD_UNIT_ID = "ca-app-pub-8360704058410758/3766067820";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_ols_home);


        adView = new AdView(this);
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId(AD_UNIT_ID);

        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .build();

        AdView adView = (AdView) this.findViewById(R.id.adView);
        adView.loadAd(adRequest);


        Button button = (Button) findViewById(R.id.button);
        Button button2 = (Button) findViewById(R.id.button2);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ols_home.this, About_Section.class));
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ols_home.this, Explore.class));
            }
        });

My Activity.xml:

<com.google.android.gms.ads.AdView android:id="@+id/adView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                ads:adUnitId="ca-app-pub-8360704058410758/3766067820"
                ads:adSize="SMART_BANNER"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true" />

My Manifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    <uses-feature android:name="android.hardware.touchscreen" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
<activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
    <activity
</application>

The Rendering Problem:

The following classes could not be instantiated: - com.google.android.gms.ads.AdView (Open Class, Show Exception) Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE Exception Details java.lang.ClassNotFoundException: com.google.android.gms.internal.bv$a   at java.lang.ClassLoader.loadClass(ClassLoader.java:306)   at java.lang.ClassLoader.loadClass(ClassLoader.java:247)   at java.lang.ClassLoader.defineClass1(ClassLoader.java:-2)   at java.lang.ClassLoader.defineClassCond(ClassLoader.java:637)   at java.lang.ClassLoader.defineClass(ClassLoader.java:621)   at java.lang.ClassLoader.defineClass(ClassLoader.java:471)   at java.lang.ClassLoader.loadClass(ClassLoader.java:306)   at java.lang.ClassLoader.loadClass(ClassLoader.java:247)   at com.google.android.gms.internal.aw.  at com.google.android.gms.internal.aw.  at com.google.android.gms.ads.AdView.  at java.lang.reflect.Constructor.newInstance(Constructor.java:513)   at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:802)   at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)   at android.view.LayoutInflater.rInflate(LayoutInflater.java:778)   at android.view.LayoutInflater.inflate(LayoutInflater.java:500)   at android.view.LayoutInflater.inflate(LayoutInflater.java:381) Copy stack to clipboard

MGames
  • 1,171
  • 2
  • 13
  • 24

1 Answers1

2

I believe that if you switch your code to flow like this is should fix this error:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_ols_home);

    // putting this line first instantiates the adView
    AdView adView = (AdView) this.findViewById(R.id.adView);

    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();

    adView.loadAd(adRequest);

It looks to me like you're trying to call your AdView before you actually instantiate it and by moving that code before the usage, you're making sure it is instantiated when you try to use it. Let us know!

NocFenix
  • 691
  • 5
  • 19
  • I removed the: AdView adView = (AdView) this.findViewById(R.id.adView); and I still get the error. What do I do? – MGames Aug 28 '14 at 01:43
  • You don't want to remove it completely. You need to move it up in the code, as in my example. – NocFenix Aug 28 '14 at 01:49
  • I moved it up and I still get the error. Am I doing something wrong? – MGames Aug 28 '14 at 01:53
  • I added the Rendering problem in my edit if it helps. – MGames Aug 28 '14 at 02:01
  • 2
    I fixed the issue! I changed the original to this: compile 'com.google.android.gms:play-services:4.+' in the dependencies part of my build.gradle. I will accept your answer for being the only one to answer for me. Please edit this into your answer, thank you. You deserve a high rep. – MGames Aug 28 '14 at 02:40