简体   繁体   English

Android Admob-找不到“ com.google.ads.AdView”类

[英]Android Admob - Didn't find class “com.google.ads.AdView”

this is my XML: 这是我的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/very_dark_gray"
        android:padding="@dimen/padding_menu_bar_items" >

        <ImageView
            android:id="@+id/ivRefresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:onClick="ivRefresh_click"
            android:padding="@dimen/padding_menu_bar_items"
            android:src="@drawable/refresh_android96" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="@string/fav"
            android:textColor="@color/light_gray"
            android:textSize="@dimen/click_text_size" />

        <ImageView
            android:id="@+id/ivOpenShareLinks"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/ivRefresh"
            android:layout_alignParentRight="true"
            android:onClick="openShareLinks"
            android:padding="@dimen/padding_menu_bar_items"
            android:src="@drawable/box_up96" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rlMainArea"
        android:layout_width="match_parent"
        android:layout_height="270dp"
        android:layout_weight="1"
        android:background="@android:drawable/sym_def_app_icon"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/llCurrentSong"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="26dp"
            android:background="@drawable/layout_bg"
            android:orientation="vertical"
            android:padding="@dimen/padding_menu_bar_items" >

            <TextView
                android:id="@+id/tvHrefStartStop"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="5.07"
                android:clickable="true"
                android:onClick="playPause"
                android:padding="@dimen/padding_menu_bar_items"
                android:text="@string/start_stop"
                android:textColor="@color/click_items"
                android:textSize="@dimen/click_text_size" />

            <TextView
                android:id="@+id/tvSongName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="5.07"
                android:padding="@dimen/padding_menu_bar_items"
                android:textColor="@color/white"
                android:textSize="@dimen/click_text_size" />

            <ProgressBar
                android:id="@+id/pbNowSong"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_weight="5.07" />

            <TextView
                android:id="@+id/tvArtist"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="5.07"
                android:padding="@dimen/padding_menu_bar_items"
                android:textColor="@color/white" />
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/rlTopArea"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_above="@+id/llCurrentSong"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="16dip"
            android:background="@drawable/layout_bg" >

            <TextView
                android:id="@+id/tvBackToMain"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="22dp"
                android:layout_toRightOf="@+id/ivBtnTopLIsten"
                android:padding="@dimen/padding_menu_bar_items"
                android:text="@string/listens"
                android:textColor="@color/click_items" />

            <ImageView
                android:id="@+id/ivBtnTopLIsten"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:onClick="clickedTopSongsIcon"
                android:padding="@dimen/padding_menu_bar_items"
                android:src="@android:drawable/ic_menu_sort_by_size" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rlEmail"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/email"
            android:onClick="clickContactUs"
            android:orientation="vertical" >
        </RelativeLayout>
    </RelativeLayout>


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:background="@drawable/lower_image" >

        <TableLayout
            android:id="@+id/TableLayoutShare"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:background="@drawable/layout_bg_white_no_radius_conrenrs"
            android:padding="@dimen/padding_menu_bar_items" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <ImageView
                    android:id="@+id/ivTwitter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:src="@drawable/tw_icon" />

                <ImageView
                    android:id="@+id/ivFacebook"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:src="@drawable/fb_icon" />
            </TableRow>

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="@string/twitter"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="@string/facebook"
                    android:textColor="@color/black" />
            </TableRow>
        </TableLayout>

        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_margin="@dimen/padding_menu_bar_items"
            android:background="@drawable/layout_bg_black_opacity_radius_corners" >
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/RelativeLayoutTodayPlaylist"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/relativeLayout1"
            android:background="@drawable/layout_bg_black_opacity_only_half_radius_corners"
            android:onClick="openListenToday" >

            <TextView
                android:id="@+id/tvListenThisWeak"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginRight="16dp"
                android:layout_toLeftOf="@+id/ivLines1"
                android:onClick="openListenToday"
                android:padding="@dimen/padding_menu_bar_items"
                android:text="@string/was_today"
                android:textColor="@color/click_items" />

            <ImageView
                android:id="@+id/ivLines1"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:onClick="clickedTopSongsIcon"
                android:padding="@dimen/padding_menu_bar_items"
                android:src="@drawable/lines" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/relativeLayout2"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/relativeLayout1"
            android:layout_alignParentRight="true"
            android:background="@drawable/layout_bg_black_opacity_only_half_radius_corners"
            android:onClick="openListenThisWeek" >

            <TextView
                android:id="@+id/tvListenToday"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginRight="16dp"
                android:layout_toLeftOf="@+id/ivLines1"
                android:onClick="openListenThisWeek"
                android:padding="@dimen/padding_menu_bar_items"
                android:text="@string/was_this_weak"
                android:textColor="@color/click_items" />

            <ImageView
                android:id="@+id/ivLines1"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:onClick="clickedTopSongsIcon"
                android:padding="@dimen/padding_menu_bar_items"
                android:src="@drawable/lines" />


        </RelativeLayout>


    </RelativeLayout>


     <com.google.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adUnitId="MY_AD_UNIT_ID"
        ads:adSize="BANNER"
        ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
        ads:loadAdOnCreate="true"/>

</LinearLayout>

I added the google.service jar in this way: moved the folder: google-play-services_lib to the main app folder. 我以这种方式添加了google.service jar:将文件夹:google-play-services_lib移至主应用程序文件夹。

then, I entered eclipsed, and imported my app > right click on the app name > Properties > Android > on the Libery Clicked 'Add' > added the Google play service: http://i.stack.imgur.com/9dHrM.png 然后,我进入了eclipsed,并导入了我的应用程序>右键单击应用程序名称>属性> Android>在Libery上单击“添加”>添加了Google Play服务: http ://i.stack.imgur.com/9dHrM 。 png

got the library in my project: http://i.stack.imgur.com/1gEq6.jpg 在我的项目中找到了该库: http : //i.stack.imgur.com/1gEq6.jpg

when I'm trying to run my app, this error appear:: 当我尝试运行我的应用程序时,出现此错误:

(26935): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.manishma/com.manishma.MainActivity}: android.view.InflateException: Binary XML file line #287: Error inflating class com.google.ads.AdView

the unitID is checked (changed the code for the example). 检查unitID(更改示例代码)。

what I'm doing wrong? 我做错了什么?

You are mixing the 2 ways possible right now to display Admob ads. 您正在混合两种可能的方式来展示Admob广告。 The first one is based on the Android SDK, the second one is based on Google Play services. 第一个基于Android SDK,第二个基于Google Play服务。

The difference between the 2 implementations are explained in details in the developers documentation 开发人员文档中详细说明了这两种实现之间的区别

In my opinion Google Play services is still too buggy to be used, but soon this will be the only option left. 我认为Google Play服务仍然无法使用,但很快它将成为唯一的选择。

In your case you mix both because you are using the XML declarations from the Android SDK but at the same time you are integrating the Google Play Services library. 在您的情况下,您将两者混合使用是因为您使用的是Android SDK中的XML声明,但同时又集成了Google Play服务库。 You cannot use both, you need to choose one and follow the developer guide I gave above. 您不能同时使用两者,需要选择一个并遵循我在上面给出的开发人员指南。

Update 更新资料

1) If you want to use Google Play services, change your XML with this: 1)如果您想使用Google Play服务,请通过以下方式更改XML:

<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"
ads:adSize="BANNER"/>

2) If you want to use the Android SDK, you have to add the GoogleAdMobAdsSdk jar in your libs folder and remove the Google Play services library. 2)如果要使用Android SDK,则必须在libs文件夹中添加GoogleAdMobAdsSdk jar并删除Google Play服务库。

Those are not the only things to do, manifest files must for example also be updated (see documentation). 这些不是唯一要做的事情,例如清单文件也必须进行更新(请参阅文档)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM