将admob代码添加到我的应用程序时

时间:2014-08-31 12:09:15

标签: android admob ads

我有异国情调的问题 我的项目没有任何错误 如何解决这个问题请求帮助 谢谢大家帮助我 大家好 我有异国情调的问题 我的项目没有任何错误 如何解决这个问题请求帮助 谢谢大家帮助我

The following classes could not be instantiated:
- com.google.android.gms.ads.AdView (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.android.gms.ads "
              android:orientation="vertical"
              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"
        ads:adSize="BANNER"
        ads:adUnitId="xxxxxxxxxxxxxxxxxxx"

        />
</LinearLayout>

的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.test.admobapp"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk
         android:minSdkVersion="9"
              android:targetSdkVersion="18" />

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

    <application 
        android:icon="@drawable/ic_launcher"
         android:label="@string/app_name">
        <activity android:name=".BannerXMLSample"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="com.google.android.gms.ads.AdActivity"
                  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

        <meta-data android:name="com.google.android.gms.version"
                   android:value="@integer/google_play_services_version" />
    </application>


</manifest>

MainAcitvity.java

package com.test.admobapp;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {
     private static final String TEST_DEVICE_ID = "xxxxxxxxxxxxxxxxxxxx";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         AdView adView = (AdView) this.findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice(TEST_DEVICE_ID)
                .build();
            adView.loadAd(adRequest);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

2 个答案:

答案 0 :(得分:0)

如果该日志消息没有更多详细信息,则表示您尚未将Google Play服务库添加到您的APK中。

答案 1 :(得分:-1)

你可以在layout.xml中使用这样的东西

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/lib/com.google.android.gms.ads"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
    </LinearLayout>
</LinearLayout>