【问题标题】:element <com.google.android.gms.ads.AdView> is unknown<com.google.android.gms.ads.AdView> 元素未知
【发布时间】:2014-06-14 13:02:14
【问题描述】:

我正在尝试将 ad-mobs 广告添加到我的 android 应用程序中我已经安装了 Google play services 17 包并将该包从 sdk>extras>google>google play service 复制到我的工作空间>myproject>lib> __ 并将 google-play-service_lib 文件导入我的工作区。 之后,我将 google-play-service.jar 添加到我的项目>java 构建路径 >libraries 中,并且我还检查了 order 和 export。

但徒劳无功,我在 main.xml 中为 Element com.google.andriod.gsm.ads.AdView 收到了一个错误,但它会在模拟器中编译并启动活动,但出现运行时错误。

我浏览了很多帖子,但没有一个能让我足够清楚。

以下是我项目的主要内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.SAI.banner"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

 <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version"/>

    <activity
        android:name="com.SAI.banner.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"/>

</application>

</manifest>

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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bg"
tools:context="com.SAI.banner.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="@string/hello_world" />

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3131326757457805/934xxxxxxx" >
</com.google.android.gms.ads.AdView>

</RelativeLayout>

MainActivity.java

package com.SAI.banner;

import android.support.v7.app.ActionBarActivity;
import com.google.android.gms.ads.*;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Look up the AdView as a resource and load a request.
    AdView adView = (AdView)this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
}
}

在此先感谢 Saikiran。 请帮我解决这个问题。

【问题讨论】:

  • 这篇文章可能对你有所帮助stackoverflow.com/questions/10523703/…
  • google play services项目必须作为库项目引用,不要只添加jar:developers.google.com/mobile-ads-sdk/docs
  • @KenWolf thanxx 建议我进行了更改,但仍然存在相同的运行时错误。
  • 还要确保您没有将片段 xml 与活动 xml 混淆。
  • @KenWolf,因为我是这个安卓 ADT 的新手,请解释一下我应该使用片段 xml 还是活动 xml。

标签: java android


【解决方案1】:

我遇到了这个问题。当我通过以下步骤添加 google play 服务时,我的问题得到了解决:

  1. 将 google Play 服务作为项目导入。不要忘记选中“将项目复制到您的工作区”选项
  2. 右键单击您的应用项目,然后单击“属性”。选择安卓。单击“添加库”。从列表中选择 google play 服务。
  3. 如果您之前已包含 Jar,则应确保删除。
  4. 清理您的项目并重新构建。

【讨论】:

  • thanxx 解决了我的问题...!! :)
猜你喜欢
  • 1970-01-01
  • 2016-04-19
  • 2019-04-27
  • 1970-01-01
  • 1970-01-01
  • 2014-06-24
  • 1970-01-01
  • 2013-06-02
  • 2013-05-22
相关资源
最近更新 更多