如何正确地配置使用AdMob的IntelliJ IDEA的?正确地、AdMob、IDEA、IntelliJ

2023-09-09 22:13:44 作者:泪染倾城

我试图将AdMob的到我的Andr​​oid应用程序。我使用的IntelliJ IDEA的发展,我似乎无法正确设置我的项目了。在这一点上我做了以下内容:

在做了所有的preliminary必要步骤下载AdMob的SDK。 在随后的方向此处,试着去增加他们的IntelliJ IDEA。我添加了一个单输入模块库依赖于AdMob的SDK到我的项目。

它看起来像IDE没有问题认识中的类SDK的,如果我尝试使用他们在code。然而,它未能解决他们的XML。我有以下两种错误:

无法解析符号AdActivity当我成立了的Andr​​oidManifest.xml 广告活动的指示要求。 元素com.google.ads.AdView在这里不允许当我尝试的广告视图添加到的方式布局文件的这里。

非常感谢您在您的帮助。我希望我已经清楚了。

修改

根据克里斯蒂安的回答澄清。这是真的,第一个错误似乎无关紧要。但是,第二个错误会导致项目生成与下面的消息打破:

... / RES /布局/ main.xml中:7:错误:错误解析XML:未绑定preFIX

在考虑中的XML是以下布局:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                机器人:方向=垂直
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT>

    < com.google.ads.AdView机器人:ID =@ + ID / AD浏览报
                           机器人:layout_width =WRAP_CONTENT
                           机器人:layout_height =WRAP_CONTENT
                           广告:adUnitId =MY_AD_UNIT_ID
                           广告:adSize =大旗
                           广告:loadAdOnCreate =真/>

    < ImageView的机器人:layout_width =WRAP_CONTENT
               机器人:layout_height =WRAP_CONTENT
               机器人:layout_alignParentTop =真
               机器人:layout_marginLeft =123dp
               机器人:SRC =@可绘制/标志/>

    < ImageView的机器人:layout_width =WRAP_CONTENT
               机器人:layout_height =WRAP_CONTENT
               机器人:layout_alignParentBottom =真
               机器人:layout_alignParentRight =真
               机器人:SRC =@可绘制/购物车/>

    <按钮机器人:ID =@ + ID / new_shopping_list
            机器人:layout_width =223dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginTop =90dp
            机器人:文本=@字符串/ new_shopping_list_btn/>

    <按钮机器人:ID =@ + ID / view_all_shopping_lists
            机器人:layout_width =223dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@ ID / new_shopping_list
            机器人:文本=@字符串/ saved_shopping_lists_btn/>

    < ImageView的机器人:ID =@ + ID / copyright_notice
               机器人:layout_width =WRAP_CONTENT
               机器人:layout_height =WRAP_CONTENT
               机器人:layout_alignParentBottom =真
               机器人:layout_alignParentRight =真
               机器人:layout_marginBottom =7DP
               机器人:layout_marginRight =5DP
               机器人:SRC =@可绘制/版权/>

    < ImageView的机器人:layout_width =WRAP_CONTENT
               机器人:layout_height =WRAP_CONTENT
               机器人:layout_above =@ ID / copyright_notice
               机器人:layout_alignParentRight =真
               机器人:layout_marginBottom =5DP
               机器人:layout_marginRight =4DP
               机器人:SRC =@可绘制/ techsmart_logo/>

    < ImageButton的机器人:ID =@ + ID / user_guide
                 机器人:layout_width =WRAP_CONTENT
                 机器人:layout_height =WRAP_CONTENT
                 机器人:layout_alignParentBottom =真
                 机器人:layout_alignParentLeft =真
                 机器人:layout_marginBottom =7DP
                 机器人:layout_marginLeft =5DP
                 机器人:SRC =@可绘制/ user_guide/>

< / RelativeLayout的>
 
IntelliJ IDEA 2020.1 正式发布,15 项重大特性 官方支持中文了 原力计划

解决方案

不要担心这些错误。 AdMob的库进行模糊处理,因此的IntelliJ无法读取类的正确名称。然而,你的应用程序编译并运行良好。这就是我的一个项目的样子,和他们很好地工作:

正如你所看到的,还有其他的库,如Pontiflex或AirPush有同样的问题。

至于你的第二个问题,看来你忘记添加XML命名空间。 This回答说,你必须添加这样一句:

 的xmlns:广告=htt​​p://schemas.android.com/apk/lib/com.google.ads
 

I'm trying to incorporate AdMob into my Android application. I'm using IntelliJ IDEA for development and I cannot seem to set my project up properly. At this point I've done the following:

Did all the preliminary steps necessary to download the AdMob SDK. Followed the directions here, trying to augment them for IntelliJ IDEA. I've added a Single-Entry Module Library dependency to the AdMob SDK to my project.

It looks like the IDE has no problem recognizing the classes from the SDK if I try to use them in code. However, it fails to resolve them in XML. I have the following two errors:

Cannot resolve symbol 'AdActivity' when I set up the ad activity in AndroidManifest.xml the instructions call for. Element com.google.ads.AdView is not allowed here when I try to add an ad view to a layout in the manner documented here.

Thank you very much in advance for your help. I hope I've been clear.

EDIT

A clarification based on Cristian's answer. It's true that the first error seems to not matter. However, the second error causes the project build to break with the following message:

.../res/layout/main.xml:7: error: Error parsing XML: unbound prefix

The XML in question is the following layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

    <com.google.ads.AdView android:id="@+id/adView"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           ads:adUnitId="MY_AD_UNIT_ID"
                           ads:adSize="BANNER"
                           ads:loadAdOnCreate="true"/>

    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentTop="true"
               android:layout_marginLeft="123dp"
               android:src="@drawable/logo"/>

    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentBottom="true"
               android:layout_alignParentRight="true"
               android:src="@drawable/cart"/>

    <Button android:id="@+id/new_shopping_list"
            android:layout_width="223dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="90dp"
            android:text="@string/new_shopping_list_btn"/>

    <Button android:id="@+id/view_all_shopping_lists"
            android:layout_width="223dp"
            android:layout_height="wrap_content"
            android:layout_below="@id/new_shopping_list"
            android:text="@string/saved_shopping_lists_btn"/>

    <ImageView android:id="@+id/copyright_notice"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentBottom="true"
               android:layout_alignParentRight="true"
               android:layout_marginBottom="7dp"
               android:layout_marginRight="5dp"
               android:src="@drawable/copyright"/>

    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_above="@id/copyright_notice"
               android:layout_alignParentRight="true"
               android:layout_marginBottom="5dp"
               android:layout_marginRight="4dp"
               android:src="@drawable/techsmart_logo"/>

    <ImageButton android:id="@+id/user_guide"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentBottom="true"
                 android:layout_alignParentLeft="true"
                 android:layout_marginBottom="7dp"
                 android:layout_marginLeft="5dp"
                 android:src="@drawable/user_guide"/>

</RelativeLayout>

解决方案

Do not worry about those errors. AdMob library is obfuscated, thus IntelliJ cannot read the correct names of the classes. However, your application will compile and run fine. This is how one of my projects looks like, and they work fine:

As you can see, there are other libraries like Pontiflex or AirPush that has the same problem.

With regards to your second problem, it seems you forget to add the XML NameSpace. This answer says that you must add this one:

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"