我有一些项目,我想在我的应用中添加Admob
个广告。但是当我在模拟器中运行我的应用程序项目时,它显示强制关闭。
这里有一些代码。
activity_main.xml中
<SearchView android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:iconifiedByDefault="true" />
<ExpandableListView
android:id="@+id/expandableList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/search" />
<com.google.android.gms.ads.adView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/expandableList"
android:layout_alignParentBottom="true" />
MainActivity.java
public class MainActivity extends Activity implements
SearchView.OnQueryTextListener, SearchView.OnCloseListener{
private SearchView search;
private mylistadapter listAdapter;
private ExpandableListView myList;
private ArrayList<continent> continentList = new ArrayList<continent>();
private AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = new AdView(this);
adView.setAdUnitId("pub-4521673227772580");
adView.setAdSize(AdSize.BANNER);
RelativeLayout layout = (RelativeLayout)findViewById(R.layout.activity_main);
layout.addView(adView);
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tesskian"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
[<uses-permission android:name="android.permission.INTERNET" />]
[<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />]
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme" >
<activity
android:name=".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.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" />
</activity>
这是日志聊天。
01-13 09:57:39.581: D/AndroidRuntime(533): Shutting down VM
01-13 09:57:39.581: W/dalvikvm(533): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-13 09:57:39.591: E/AndroidRuntime(533): FATAL EXCEPTION: main
01-13 09:57:39.591: E/AndroidRuntime(533): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tesskian/com.example.tesskian.MainActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class com.google.android.gms.ads.adView
01-13 09:57:39.591: E/AndroidRuntime(533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.os.Looper.loop(Looper.java:137)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-13 09:57:39.591: E/AndroidRuntime(533): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 09:57:39.591: E/AndroidRuntime(533): at java.lang.reflect.Method.invoke(Method.java:511)
01-13 09:57:39.591: E/AndroidRuntime(533): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-13 09:57:39.591: E/AndroidRuntime(533): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-13 09:57:39.591: E/AndroidRuntime(533): at dalvik.system.NativeStart.main(Native Method)
01-13 09:57:39.591: E/AndroidRuntime(533): Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class com.google.android.gms.ads.adView
01-13 09:57:39.591: E/AndroidRuntime(533): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-13 09:57:39.591: E/AndroidRuntime(533): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.app.Activity.setContentView(Activity.java:1835)
01-13 09:57:39.591: E/AndroidRuntime(533): at com.example.tesskian.MainActivity.onCreate(MainActivity.java:41)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.app.Activity.performCreate(Activity.java:4465)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-13 09:57:39.591: E/AndroidRuntime(533): ... 11 more
01-13 09:57:39.591: E/AndroidRuntime(533): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.ads.adView
01-13 09:57:39.591: E/AndroidRuntime(533): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
01-13 09:57:39.591: E/AndroidRuntime(533): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-13 09:57:39.591: E/AndroidRuntime(533): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
01-13 09:57:39.591: E/AndroidRuntime(533): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
01-13 09:57:39.591: E/AndroidRuntime(533): ... 21 more
我不知道解决这个问题。任何人都可以帮我解决这个问题吗?