问题描述
我正在尝试在我的应用中实现 admob,但添加后应用无法启动.
I'm trying to implement admob in my app, but the app failed to start after I added it.
这是活动代码:http:///code.google.com/p/zhuang-dict/source/browse/trunk/ZhuangDict/src/cn/wangdazhuang/zdict/ZhuangDictActivity.java
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads= "http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageButton
android:id="@+id/ts"
android:contentDescription="@string/tts_contentDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@android:drawable/ic_btn_speak_now"
/>
<ImageButton
android:id="@+id/search"
android:contentDescription="@string/search_contentDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ts"
android:src="@android:drawable/ic_search_category_default"
/>
<AutoCompleteTextView
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:singleLine="true"
android:layout_toLeftOf="@id/search"
android:hint="@string/edit_hint"
/>
</RelativeLayout>
<WebView
android:id="@+id/result"
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="myId"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
/>
</LinearLayout>
推荐答案
您是否已将此添加到您的清单中?
Have You added this to Your Manifest?
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
如谷歌教程所示:
<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:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
adUnitId 必须是您应用的 ID,您是否设置了此 ID,或者您是否真的像上面显示的代码那样设置了myid"?
The adUnitId has to be the Id for Your app, did You set this ID or did You really set "myid" like in your code shown above?
这里有一些附加信息,所有似乎都是 AdMob Lib 未绑定到您的项目.这可能是由许多不同的原因造成的.阅读步骤并确定,你真的做到了,如果没有,那就去做吧:
Here is some additional information, All seems to be that the AdMob Lib is not bundled to Your project. This could cause of many different reasons. Read the steps and get sure, You have really done this, if not, do it:
- 以管理员身份打开 Eclipse
- 转到帮助-->检查更新并运行更新
- 设置所有已检查的内容,显示在下一个窗口中.没必要,但也不要全部更新
- 接受条款和被许可方,然后按完成...更新将开始,需要一些时间
- 如果更新完成,请以管理员身份重新启动 Eclipse.您可能会收到一条错误消息,指出并非所有更新都可以完成...继续
更新 Android SDK:
- 在 Eclipse 中转到 Window-->Android SDK Manager
- 在状态"列中,您可以查看更新是否可用.选择所有更新...
- 如果完成,请再次重启 Eclipse
使用 Admob - 放置 jar 文件并设置路径:
- 从 Google 网站下载 AdMob SDK(可能是不同的链接,具体取决于国家/地区)https://developers.google.com/mobile-ads-sdk/download?hl=de
- 将文件解压到一个目录中(无论在哪里)
在 Eclispe 的项目中创建一个libs"文件夹.确保文件夹被命名为libs"而不是lib".这个错误经常发生
- download AdMob SDK from Google site(could be a different link depends on country) https://developers.google.com/mobile-ads-sdk/download?hl=de
- unzip the file into a directory (no matter where)
create a "libs" folder inside your project in Eclispe. BE SURE THAT THE FOLDER IS NAMED "libs" AND NOT "lib". THIS MISTAKE IS OFTEN DONE
将 jar 文件从 admob 文件夹复制到您的项目的 libs 文件夹中
copy the jar file from admob folder into Your project into the libs folder
开始
把它放到你的 Manifest.xml 中
put this into your Manifest.xml
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
您的 Manifest.xml 应该类似于:
Your Manifest.xml should look similar to this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company"
android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true">
<activity android:label="@string/app_name" android:name="BannerExample">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>
注意:将此行放在您的活动下方,而不是内部
NOTE: Put this line above under Your activity, not inside
在 manifest.xml 中设置权限
set permissions inside the manifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
创建 xml 布局,例如教程中所示
create the xml layout for example like shown in the tutorial
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
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:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
</LinearLayout>
此外,如果上述步骤不起作用,您可以在您想要显示 admob 的 Activity 中执行此操作.请参阅您的 Activity 的 onCreate() 中的您的 admob xml id:
Additionally, if the steps above did not work, You could do this inside Your Activity where You want to show the admob. Refer to Your admob xml id in onCreate() of Your Activity:
public class AdMobActivity extends Activity {
private AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the adView
adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
// Lookup your LinearLayout assuming it’s been given
// the attribute android:id="@+id/mainLayout"
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
}
在我的应用程序中,我都做了,通常只需要在 XML 或代码中做,但两者都可以正常工作.
In my App, I have done both, usually it is only necessary to do it in XML or Code, but with both it works fine too.
这篇关于添加admob后Android应用无法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!