应用程序每次运行时都会崩溃

时间:2013-04-01 06:24:43

标签: android admob

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>

SimpleBanerAd Manifest

<?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模拟器上运行它。我哪里错了?

1 个答案:

答案 0 :(得分: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" />