强制关闭与AdMob的;开发的Android 2.2 [英] Force Close with AdMob; developing for android 2.2
问题描述
我一直在努力,在过去两天了实施Admob的广告在其中正在为Android 2.2及以上开发的应用程序。我已在网上搜索并尝试了所有我已经找到了答案,但仍然一无所获。我也跟着上了AdMob的网站上所有的指示,并尝试了Java(贴在下面)和XML版本。每次我使用XML,我得到的错误未能实例化com.google.ads.AdView。我曾尝试修复了一些漏洞,但它仍然崩溃。到目前为止,我已经改变了的minSdkVersion8(用于开发在2.2),和目标到Android = 14,这是需要的AdMob的SDK。任何帮助将是非常美联社preciated!
I have been trying for the past two days now to implement Admob ads into an app which is being developed for Android 2.2 and above. I have searched online and tried all of the answers I have found, but still, nothing. I followed all the instructions on the admob site, and have tried both the java (posted below) and the xml version. Everytime I use xml, I get the error "failed to instantiate com.google.ads.AdView". I have tried the fixes for that but it still crashes. So far I have changed the "minSdkVersion" to 8 (for developing on 2.2), and the "target" to android=14, which is needed for the AdMob sdk. Any help would be much appreciated!
Menu类
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
public class Menu extends Activity{
Button start, HTP;
private AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button) findViewById(R.id.bStart);
HTP = (Button) findViewById(R.id.bHTP);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Menu.this,
MyGameActivity.class);
Menu.this.startActivity(myIntent);
}
});
HTP.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Menu.this,
HowToPlay.class);
Menu.this.startActivity(myIntent);
}
});
// Create the adView
adView = new AdView(this, AdSize.BANNER, "publisher id in quotes");
// 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());
}
@Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tacomaapps.game"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:icon="@drawable/ic_game"
android:label="Cube Jump" >
<activity
android:name=".Splash"
android:label="Cube Jump"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="Cube Jump"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="com.tacomaapps.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MyGameActivity"
android:label="Cube Jump"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="landscape" >
</activity>
<activity
android:name=".HowToPlay"
android:label="Cube Jump"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="landscape" >
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
的main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/mainLayout"
android:layout_height="match_parent"
android:background="@drawable/menuback"
android:baselineAligned="true"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="210dp"
android:layout_weight="1.0"
android:weightSum="100" >
<Button
android:id="@+id/bFillSpace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="45"
android:visibility="invisible"
android:text="Button" />
<Button
android:id="@+id/bStart"
android:layout_width="150dp"
android:layout_height="63dp"
android:layout_margin="5dp"
android:layout_weight="5"
android:background="@drawable/play" />
<Button
android:id="@+id/bHTP"
android:layout_width="150dp"
android:layout_height="63dp"
android:layout_margin="5dp"
android:layout_weight="5"
android:background="@drawable/htp_button" />
<Button
android:id="@+id/bFillSpace2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="45"
android:visibility="invisible"
android:text="Button" />
</LinearLayout>
推荐答案
您code是完全正常的,并运行在我结束。
Your code is perfectly fine and runs at my end.
我想你检查下嘛。
1 您已经创建了库文件夹中的项目,并添加GoogleAdMobAdsSdk-XXXjar有并从there.By默认引用它,它们不存在。如果您是直接引用从\额外的jar文件\谷歌\ admob_ads_sdk然后程序崩溃与 java.lang.NoClassDefFoundError的:com.google.ads.AdView
奇怪但却真实。
1 You have created "libs" folder in project and added GoogleAdMobAdsSdk-X.X.X.jar there and reference it from there.By default, they doesn't exist.If you are directly referencing jar file from \extras\google\admob_ads_sdk then program crashes with java.lang.NoClassDefFoundError: com.google.ads.AdView
Strange but true.
您的项目结构应该是这个样子。
You project structure should look something like this
这篇关于强制关闭与AdMob的;开发的Android 2.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!