Android AdRequest.Builder 无法解析为类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20607746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AdRequest.Builder cannot be resolved to a type
提问by Nazerke
I'm incorporating AdMob into my app. I've followed the steps in the developers page. However AdRequest.Builder() is being underlined with red and it says:
我正在将 AdMob 整合到我的应用中。我已按照开发人员页面中的步骤进行操作。但是 AdRequest.Builder() 带有红色下划线,它说:
AdRequest cannot be resolved to a type
AdRequest 无法解析为类型
and
和
AdRequest.Builder cannot be resolved to a type.
AdRequest.Builder 无法解析为类型。
What could be the problem?
可能是什么问题呢?
import com.google.ads.AdRequest;
import com.google.ads.AdView;
public class FireRoomActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fire_room);
// 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);
}
In xml I've shown admob as such:
在 xml 中,我显示了 admob:
<com.google.android.gms.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="bla bla"
ads:adSize="BANNER"/>
回答by itvdonsk
Your code is mix for Admob SDK (Google Play)and Android (6.4.1 and earlier SDKs)
您的代码适用于Admob SDK (Google Play)和Android(6.4.1 及更早的 SDK)
Use
用
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
AdRequest adRequest = new AdRequest.Builder().build();
and
和
<com.google.android.gms.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="bla bla"
ads:adSize="BANNER"/>
if you use Admob SDK (Google Play)
如果您使用 Admob SDK (Google Play)
Or use
或使用
import com.google.ads.AdRequest;
import com.google.ads.AdView;
AdRequest adRequest = new AdRequest();
and
和
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="bla bla"
ads:adSize="BANNER"/>
if you use earlies SDK
如果你使用早期的 SDK
For Admob SDK (Google Play) not forget to change namespase
对于 Admob SDK (Google Play) 不要忘记更改命名空间
xmlns:ads="http://schemas.android.com/apk/res-auto"
回答by Hariharan
Try this..
尝试这个..
AdRequest adRequest = new AdRequest();
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(adRequest);
Note:
笔记:
Make sure you have included the necessary
library
in yourproject
and permissionsin yourmanifest
.Also check whether you have given correct
ad-mob
id in your xml.
确保您已将必要
library
的project
和权限包含在您的manifest
.还要检查您是否
ad-mob
在xml 中给出了正确的id 。
EDIT :
编辑 :
To add test device, you could try
要添加测试设备,您可以尝试
adRequest.addTestDevice(AdRequest.TEST_EMULATOR); //for emulators
adRequest.addTestDevice("device_id"); //for real device, enter device id
回答by S.Ahsan
Probably you are encountering Library Reference Bug of Eclips
. Which returns this Types of error on following developers page's Steps.
可能您遇到了Eclips
. 在以下开发人员页面的步骤中返回此类型的错误。
Go To your Project Properties and Click on Android Tab.
转到您的项目属性并单击 Android 选项卡。
Check, if a Red
cross mark exists.
检查是否Red
存在十字标记。
if yes, you are encountering it for sure.
如果是,您肯定会遇到它。
Eclipse does weird things when importing an existing project (google-play-services-lib), especially if you try to import and then allow the project to be automatically 'copied' to your workspace.
Eclipse 在导入现有项目 (google-play-services-lib) 时会做一些奇怪的事情,尤其是当您尝试导入然后允许项目自动“复制”到您的工作区时。
- Erase all google-play-services projects from your workspace.
- Close Eclipse.
- Manually copy the google-play-services-lib folder (....sdk\extras\google\google_play_services\libproject\google-play-services_lib) into your workspace.
- Open Eclipse
- 从您的工作区中删除所有 google-play-services 项目。
- 关闭日食。
- 手动将 google-play-services-lib 文件夹 (....sdk\extras\google\google_play_services\libproject\google-play-services_lib) 复制到您的工作区。
- 打开日食
Now add the Library From your workspace as existing project
instead of
现在添加Library From your workspace as existing project
而不是
....sdk\extras\google\google_play_services\libproject\google-play-services_lib
At last, add project reference library from project properties as you did before.
最后,像之前一样从项目属性中添加项目引用库。
Remember, directions on developers page is fully perfect without this single bug. So, Follow All other directions As the Page says.
请记住,开发人员页面上的说明是完全完美的,没有这个错误。所以,按照页面所说的所有其他方向。