-5

activity_main.xml

<RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">   

     <TextView 
         android:layout_height="wrap_content" 
         android:layout_width="fill_parent" 
         android:text="@string/hello_world"/>

     <com.admob.android.ads.AdView
         android:id="@+id/ad"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         ads:adSize="BANNER"
         ads:adUnitId="***************"
         ads:loadAdOnCreate="true"
         ads:testDevices="TEST_EMULATOR" /> </RelativeLayout>

SimpleBaner 广告清单

<?xml version="1.0"
 encoding="utf-8"?> <manifest
 xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.simplebanerad"
     android:versionCode="1"
     android:versionName="1.0" >

     <uses-sdk
         android:minSdkVersion="13"
         android:targetSdkVersion="17" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
     <uses-permission android:name="android.permission.INTERNET"/>

     <application
         android:allowBackup="true"
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/AppTheme" >
         <activity
             android:name="com.example.simplebanerad.MainActivity"
             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.ads.AdActivity"                 android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
         </activity>
     </application>

 </manifest>

MainActivity.java 包 com.example.simplebanerad;

 import android.os.Bundle; import android.app.Activity; import
 android.view.Menu;

 public class MainActivity extends Activity { @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
     }


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

我想展示广告,但每次运行我的应用程序都会崩溃。我在 4.2.2 API 级别 17 模拟器上运行它。我哪里错了?

4

1 回答 1

0

膨胀类 com.admob.android.ads.AdView 时出错

您收到此错误,因为 AdView 类不在com.admob.android.ads包中,它在com.google.ads包中。

所以改变这个:

<com.admob.android.ads.AdView
         android:id="@+id/ad"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         ads:adSize="BANNER"
         ads:adUnitId="***************"
         ads:loadAdOnCreate="true"
         ads:testDevices="TEST_EMULATOR" />

对此:

<com.google.ads.AdView
         android:id="@+id/ad"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         ads:adSize="BANNER"
         ads:adUnitId="***************"
         ads:loadAdOnCreate="true"
         ads:testDevices="TEST_EMULATOR" />
于 2013-04-01T07:04:49.270 回答