我正在尝试通过 Google Play 服务在 Android 应用程序中实现 Admob。
我已经在 list 文件中声明了广告 Activity ,但仍然收到错误:AdActivity with ...未在 list 文件中声明。
这是我到目前为止所做的事情:
xml 文件是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AddActivity"
android:id="@+id/adView">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
Java 文件是:
package com.example.adtesting;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.RelativeLayout;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public class AddActivity extends Activity {
final String adUnitId ="pub-*******";
private AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
// create adView
adView = new AdView(this);
adView.setAdUnitId(adUnitId);
adView.setAdSize(AdSize.BANNER);
//Lookup Linear Layout
RelativeLayout layout = (RelativeLayout) findViewById(R.id.adView);
//add AdView to it
layout.addView(adView);
//initiate generic request
AdRequest adRequest = new AdRequest.Builder().build();
// Load the adView withe the add request
adView.loadAd(adRequest);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add, menu);
return true;
}
}
list 文件是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.adtesting"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<activity
android:name="com.example.adtesting.AddActivity"
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.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|
screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>
请注意,我引用了 Google Play 服务 SDK。
感谢您的帮助!
您已经声明了该包:
package="com.example.adtesting"
所以你只需要: android:name=".AddActivity"
<activity
android:name=".AddActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我是一名优秀的程序员,十分优秀!