【发布时间】:2016-03-28 16:43:01
【问题描述】:
我想在应用程序中添加一个广告。所以我已经阅读了来自
的说明我应用了所有步骤,但是当我运行我的应用程序时,我得到了:
android.View.InflateException:二进制 Xml 文件行 xx 错误 膨胀类 com.google.android.gms.ads.Adview
我不知道我必须做什么
Activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/ColorPrimary"
android:elevation="4dp"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
xmlns:android="http://schemas.android.com/apk/res/android" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
></android.support.v4.view.ViewPager>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.proje.exa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@drawable/app_icon2"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<provider
android:name=".ContentProvider"
android:authorities="com.proje.exa.ContentProvider"></provider>
<activity
android:name="com.proje.exa.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.DEFAULT" />
<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|s creenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
</manifest>
在 Mainactivity.java 中
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends AppCompatActivity{
......
......
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
我包含了这些行,但在运行应用程序时出现错误
不知道怎么回事,谢谢帮忙..
【问题讨论】:
-
请尝试清理项目。然后查看是否在构建路径中检查了所需的库。让我知道这是否能解决问题
-
错误来自用户界面,可能是因为你没有关闭你的 RelativeLayout。如果 rahul 说的没有用,可能是 --android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"--
-
我已经清理了项目,并查看了 Java 构建路径中的“库”选项卡。 Android 依赖项部分有 google_play_services_lib.jar,Android 私有库部分有 google-play-services.jar,但我没有添加任何外部 jar。我需要将此 google-play-services.jar 添加为外部 jar 吗?
标签: java android xml eclipse admob