我正在使用Android应用,我正在尝试将google addmob添加到我的应用中。并且我的代码中没有错误,但应用程序似乎在运行时强制关闭。我找不到错误。请帮我, 我用了代码 -
adView = new AdView(this, AdSize.BANNER, "my add id");
RelativeLayout layout = (RelativeLayout)findViewById(R.id.adView);
layout.addView(adView);
AdRequest request = new AdRequest();
request.setTesting(false);
adView.loadAd(request);
请帮助我,因为我是Android开发的新手
logcat的:
11-09 17:26:43.508: E/AndroidRuntime(8982): at android.app.Activity.setContentView(Activity.java:1862)
11-09 17:26:43.508: E/AndroidRuntime(8982): Caused by: java.lang.ClassNotFoundException: com.google.ads.AdView
答案 0 :(得分:0)
// try this
1. download latest google-play-services,jar
2. add jar to project build path
**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:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/lnrMain">
</LinearLayout>
public class MyActivity extends FragmentActivity {
LinearLayout lnrMain;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lnrMain = (LinearLayout) findViewById(R.id.lnrMain);
runOnUiThread(new Runnable() {
@Override
public void run() {
AdView adView = new AdView(MyActivity.this);
adView.setAdUnitId("0445b7141d9d4e1b");
adView.setAdSize(AdSize.BANNER);
AdRequest.Builder builder = new AdRequest.Builder();
builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
adView.loadAd(builder.build());
lnrMain.addView(adView);
}
});
}
}
**AndroidManifest.xml**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Demo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<activity
android:name="MyActivity"
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" />
<meta-data
android:name="ADMOB_ALLOW_LOCATION_FOR_ADS"
android:value="true" />
<meta-data
android:name="com.google.android.gms.version"
android:value="4030500" />
</application>
</manifest>
答案 1 :(得分:0)
昨天我将我的第一个应用程序与admob集成在一起,我所做的就是将.jar库添加到Java Build Path中。在Manifest文件中,我在关闭应用程序标记上面添加了这些行:
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
和这些权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
这就是我的Manifest.xml的样子:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="PACKAGENAME"
android:versionCode="1"
android:versionName="1.1" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<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>
在我的main_acitivty.xml中,我添加了这些代码行,我实际上想看到我的广告横幅:
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="YOU-ID"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
我实际上没有在.java文件中写任何代码。因为您可以选择通过XML或以编程方式实现AdMob。
这里提供了所有这些步骤 - &gt; AdMob Banners
答案 2 :(得分:0)
here is my code am used
****MainActivity.java`
package com.example.smacon;
import com.google.ads.*;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Surface;
import android.webkit.WebView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
public class MainActivity extends Activity {
private AdView adView;
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdRequest adRequest = new AdRequest();
//test mode on EMULATOR
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
//test mode on DEVICE (this example code must be replaced with your device uniquq ID)
adRequest.addTestDevice("4G74FC73D62D42B62A7F7DA61EF5F776");
adView = (AdView)findViewById(R.id.adView);
// Initiate a request to load an ad in test mode.
// You can keep this even when you release your app on the market, because
// only emulators and your test device will get test ads. The user will receive real ads.
adView.loadAd(adRequest);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("my url");
webView.setWebViewClient(new MyWebViewClient());
}
@Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
***** MyWebViewClient.java
package com.example.smacon;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebViewClient extends WebViewClient {
// show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// webProg.setVisibility(View.GONE);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
**** manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smacon"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.smacon.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.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode
|screenSize|smallestScreenSize" >
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
****activity_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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
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="7565756565"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
</LinearLayout>
no errors in this code. But it is forced closed . :(