Android Studio:无法实例化以下类: - com.google.android.gms.ads.AdView(Open Class,Show Exception)

时间:2014-08-28 00:47:51

标签: java android admob

广告在模拟器上完美展示,但我仍然在设备预览中看到此错误。此错误会阻止预览出现渲染问题。我已经尝试了所有的东西,没有任何效果。这是我的.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));
            }
        });

我的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" />

我的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>

渲染问题:

无法实例化以下类: - com.google.android.gms.ads.AdView(Open Class,Show Exception)  提示:在自定义视图中使用View.isInEditMode()可以在IDE异常详细信息java.lang.ClassNotFoundException:java.lang中的com.google.android.gms.internal.bv $ a中显示时跳过代码或显示示例数据。 java.lang.ClassLoader.defineClassCond上的java.lang.ClassLoader.defineClass1(ClassLoader.java:-2)中的java.lang.ClassLoader.loadClass(ClassLoader.java:247)中的ClassLoader.loadClass(ClassLoader.java:306) ClassLoader.java:637)java.lang.ClassLoader.defineClass(ClassLoader.java:621)java.lang.ClassLoader.defineClass(ClassLoader.java:471)java.lang.ClassLoader.loadClass(ClassLoader.java:306) )在com.google.android.gms.internal.aw的java.lang.ClassLoader.loadClass(ClassLoader.java:247)。在com.google.android.gms.internal.aw。在com.google.android.gms.ads.AdView上。在java.lang.reflect.Constructor.newInstance(Constructor.java:513)的android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:802)android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)。在android.view.LayoutInflater.inflate(LayoutInflater.java:381)的android.view.LayoutInflater.inflate(LayoutInflater.java:500)中查看.LayoutInflater.rInflate(LayoutInflater.java:778)将堆栈复制到剪贴板

1 个答案:

答案 0 :(得分:2)

我相信如果您将代码切换为流程,则应该修复此错误:

@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);

我认为,在您实际实例化之前尝试调用AdView,并在使用之前移动该代码,我们确保在您尝试使用它时将其实例化。请告诉我们!