1

我在我的主应用程序中使用 AdMob。但是当我启动一些服务时,我得到了异常:android.view.InflateException: Binary XML file line #134: Error inflating class <unknown>

mainAdMob 布局

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="bottom"
        android:orientation="vertical" >

        <com.google.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
            android:id="@+id/adView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="xxx"
            android:gravity="bottom" />
    </LinearLayout>

然后当我运行调用此代码的服务时:

private View getMainLayout() {
        LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.main, null);

        return layout;
    }

我得到例外:

01-26 07:55:56.500: W/dalvikvm(21075): threadid=1: thread exiting with uncaught exception (group=0x40c7a1f8)
01-26 07:55:56.655: E/AndroidRuntime(21075): FATAL EXCEPTION: main
01-26 07:55:56.655: E/AndroidRuntime(21075): java.lang.RuntimeException: Unable to create service com.xxx.sss.service.SmsService: android.view.InflateException: Binary XML file line #134: Error inflating class <unknown>
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2274)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.app.ActivityThread.access$1600(ActivityThread.java:127)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.os.Looper.loop(Looper.java:137)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.app.ActivityThread.main(ActivityThread.java:4511)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at java.lang.reflect.Method.invokeNative(Native Method)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at java.lang.reflect.Method.invoke(Method.java:511)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at dalvik.system.NativeStart.main(Native Method)
01-26 07:55:56.655: E/AndroidRuntime(21075): Caused by: android.view.InflateException: Binary XML file line #134: Error inflating class <unknown>
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at com.xxx.sss.service.SmsService.getMainLayout(SmsService.java:79)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at com.xxx.sss.service.SmsService.setOnLabelOnButton(SmsService.java:68)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at com.xxx.sss.service.SmsService.onCreate(SmsService.java:37)
01-26 07:55:56.655: E/AndroidRuntime(21075):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2264)

怎么了?

4

2 回答 2

1

您也可以尝试添加依赖项:将 'com.google.android.gms:play-services-ads:8.4.+' 编译到您的应用级别 build.gradel 并构建项目。

然后在你的 layout.xml 文件中添加

<com.google.android.gms.ads.AdView
                    android:id="@+id/adView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    ads:adSize="BANNER"
                    ads:adUnitId="@string/banner_ad_unit_id"/>

banner_ad_unit_id:这必须是您的唯一 ID

将波纹管添加到您的 java 文件 java 文件

mAdView = (AdView) mMainView.findViewById(R.id.adView); 

不要忘记在清单文件中添加AdActivity 。

于 2016-08-24T09:06:32.597 回答
0

您是否忘记com.google.ads.AdView在清单文件中声明库?

于 2013-01-26T07:12:24.917 回答