广告(智能横幅admob
)最近在我的应用程序中消失了,尽管它们之前工作正常。
在Eclipse
中使用virtual device
运行应用时会显示以下消息:
无法接收广告(广告请求无效。)
当我打开main.xml
文件时,会显示以下错误消息:
为标记LinearLayout
找到了意外的名称空间前缀“xmlns”
文件:
main.xml中
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fullwrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#5a4872" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#5a4872" >
<LinearLayout
android:id="@+id/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splashscreen"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<Button
android:id="@+id/button_play"
android:background="@drawable/btn_gameplay"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="horizontal"
android:text="@string/button_play"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="25.0sp" />
<ImageView
android:id="@+id/image_splash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/appname"
android:gravity="center_horizontal" />
</LinearLayout>
<view
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="maazbah.memory.game.kids.ui.MemoryGridView"
android:gravity="center"
android:horizontalSpacing="8dp"
android:layoutAnimation="@anim/layout_random_fade"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="8dp"
android:visibility="gone" />
</LinearLayout>
<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="wrap_content"
android:layout_gravity="bottom"
android:gravity="center" >
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="@string/banner_ad_unit_id"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"
/>
</LinearLayout>
</LinearLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="auto"
package="maazbah.memory.game.kids">
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:logo="@drawable/logo"
android:hardwareAccelerated="true">
<activity android:name="maazbah.memory.game.kids.ui.MainActivity"
android:screenOrientation="portrait"
android:label="@string/activity_title" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="maazbah.memory.game.kids.ui.PreferencesActivity"
android:screenOrientation="portrait"
android:label="@string/activity_title" >
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>
MainActivity.java
package maazbah.memory.game.kids.ui;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import maazbah.memory.game.kids.Constants;
import maazbah.memory.game.kids.PreferencesService;
import maazbah.memory.game.kids.R;
import maazbah.memory.game.kids.Rotate3dAnimation;
import org.androidsoft.utils.sound.SoundManager;
/**
* AbstractMainActivity
*/
public abstract class AbstractMainActivity extends Activity implements OnClickListener
{
private static final String PREF_STARTED = "started";
private static final int SOUND_NEW_GAME = 1000;
private static final int SPLASH_SCREEN_ROTATION_COUNT = 2;
private static final int SPLASH_SCREEN_ROTATION_DURATION = 2000;
private static final int GAME_SCREEN_ROTATION_COUNT = 2;
private static final int GAME_SCREEN_ROTATION_DURATION = 2000;
private static final String KEY_VERSION = "version";
private static final int DEFAULT_VERSION = 1; // should be set to 0 after 1.4
protected boolean mQuit;
private ViewGroup mContainer;
private View mSplash;
private Button mButtonPlay;
private boolean mStarted;
protected abstract View getGameView();
protected abstract void newGame();
protected abstract void preferences();
/**
* {@inheritDoc }
*/
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
SoundManager.init(AbstractMainActivity.this);
setContentView(R.layout.main);
mContainer = (ViewGroup) findViewById(R.id.container);
mSplash = (View) findViewById(R.id.splash);
mButtonPlay = (Button) findViewById(R.id.button_play);
mButtonPlay.setOnClickListener(this);
checkLastVersion();
}
/**
* {@inheritDoc }
*/
@Override
protected void onResume()
{
super.onResume();
SharedPreferences prefs = getPreferences(0);
mStarted = prefs.getBoolean(PREF_STARTED, false);
if (mStarted)
{
mSplash.setVisibility(View.GONE);
getGameView().setVisibility(View.VISIBLE);
} else
{
mSplash.setVisibility(View.VISIBLE);
getGameView().setVisibility(View.GONE);
}
if (!SoundManager.isInitialized())
{
SoundManager.init(this);
}
SoundManager.instance().addSound(SOUND_NEW_GAME, R.raw.start_game);
}
/**
* {@inheritDoc }
*/
@Override
protected void onPause()
{
super.onPause();
SharedPreferences.Editor editor = getPreferences(0).edit();
if (!mQuit)
{
editor.putBoolean(PREF_STARTED, mStarted);
} else
{
editor.remove(PREF_STARTED);
}
editor.commit();
SoundManager.release();
}
/**
* {@inheritDoc }
*/
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
/**
* {@inheritDoc }
*/
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_new:
onNewGame();
return true;
case R.id.menu_quit:
quit();
return true;
case R.id.menu_preferences:
preferences();
return true;
}
return false;
}
private void onNewGame()
{
if( PreferencesService.instance().isSoundEnabled() )
{
SoundManager.instance().playSound(SOUND_NEW_GAME);
}
newGame();
}
/**
* Quit the application
*/
void quit()
{
mQuit = true;
AbstractMainActivity.this.finish();
}
/**
* {@inheritDoc }
*/
public void onClick(View v)
{
if (v == mButtonPlay)
{
applyRotation(0, SPLASH_SCREEN_ROTATION_COUNT * 360);
}
}
protected void showEndDialog(String title, String message, int icon)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setIcon(icon);
builder.setMessage(message);
builder.setCancelable(false);
builder.setPositiveButton(getString(R.string.new_game),
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
onNewGame();
}
});
builder.setNegativeButton(getString(R.string.quit),
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
quit();
}
});
AlertDialog alert = builder.create();
alert.show();
}
// 3D anim from Splash Screen to Game screen
/**
* Setup a new 3D rotation on the container view.
*
* @param start the start angle at which the rotation must begin
* @param end the end angle of the rotation
*/
private void applyRotation(float start, float end)
{
// Find the center of the container
final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;
// Create a new 3D rotation with the supplied parameter
// The animation listener is used to trigger the next animation
final Rotate3dAnimation rotation =
new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true);
rotation.setDuration(SPLASH_SCREEN_ROTATION_DURATION);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView());
mContainer.startAnimation(rotation);
}
/**
* This class listens for the end of the first half of the animation.
* It then posts a new action that effectively swaps the views when the container
* is rotated 90 degrees and thus invisible.
*/
private final class DisplayNextView implements Animation.AnimationListener
{
private DisplayNextView()
{
}
public void onAnimationStart(Animation animation)
{
if( PreferencesService.instance().isSoundEnabled() )
{
SoundManager.instance().playSound(SOUND_NEW_GAME);
}
}
public void onAnimationEnd(Animation animation)
{
mContainer.post(new SwapViews());
}
public void onAnimationRepeat(Animation animation)
{
}
}
/**
* This class is responsible for swapping the views and start the second
* half of the animation.
*/
private final class SwapViews implements Runnable
{
public void run()
{
final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;
Rotate3dAnimation rotation;
mSplash.setVisibility(View.GONE);
getGameView().setVisibility(View.VISIBLE);
getGameView().requestFocus();
rotation = new Rotate3dAnimation(0, 360 * GAME_SCREEN_ROTATION_COUNT, centerX, centerY, 310.0f, false);
rotation.setDuration(GAME_SCREEN_ROTATION_DURATION);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
mContainer.startAnimation(rotation);
mStarted = true;
}
}
private void checkLastVersion()
{
int resTitle;
int resMessage;
final int lastVersion = getVersion();
if (lastVersion < Constants.VERSION)
{
if (lastVersion == 0)
{
// This is a new install
resTitle = R.string.first_run_dialog_title;
resMessage = R.string.first_run_dialog_message;
} else
{
// This is an upgrade.
resTitle = R.string.whats_new_dialog_title;
resMessage = R.string.whats_new_dialog_message;
}
// show what's new message
saveVersion(Constants.VERSION);
showWhatsNewDialog(resTitle, resMessage, R.drawable.icon);
}
}
private int getVersion()
{
SharedPreferences prefs = getSharedPreferences(AbstractMainActivity.class.getName(), Activity.MODE_PRIVATE);
return prefs.getInt(KEY_VERSION, DEFAULT_VERSION);
}
private void saveVersion(int version)
{
SharedPreferences prefs = getSharedPreferences(AbstractMainActivity.class.getName(), Activity.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt(KEY_VERSION, version);
editor.commit();
}
protected void showWhatsNewDialog(int title, int message, int icon)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setIcon(icon);
builder.setMessage(message);
builder.setPositiveButton(getString(R.string.ok),
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
onNewGame();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
有谁知道我的代码出了什么问题? 感谢。
答案 0 :(得分:0)
删除
来自嵌套LinearLayout的xmlns:android="http://schemas.android.com/apk/res/android"
。只保留在根LinearLayout(第一个)中。因为Android.xml文件中的Android命名空间应该只声明一次。
答案 1 :(得分:0)
将LinearLayout中的广告命名空间更改为
xmlns:ads="http://schemas.android.com/apk/res-auto"
答案 2 :(得分:0)
感谢Rebly亲爱的Shubham Shukla&amp;雨披,
我已从嵌套的linearlayout中删除了这一行。
xmlns:android="http://schemas.android.com/apk/res/android"
并仅将其保留在root linearlayout中, 我将此行移至root linearlayout。
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
文件(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:id="@+id/fullwrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#5a4872" >
<LinearLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#5a4872" >
<LinearLayout
android:id="@+id/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splashscreen"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<Button
android:id="@+id/button_play"
android:background="@drawable/btn_gameplay"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="horizontal"
android:text="@string/button_play"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="25.0sp" />
<ImageView
android:id="@+id/image_splash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/appname"
android:gravity="center_horizontal" />
</LinearLayout>
<view
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="maazbah.memory.game.kids.ui.MemoryGridView"
android:gravity="center"
android:horizontalSpacing="8dp"
android:layoutAnimation="@anim/layout_random_fade"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="8dp"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center" >
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub-9321782199225340/4561186116"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"
/>
</LinearLayout>
</LinearLayout>
以及在此修订后eclips中显示的新消息,
To get test ads on this device, call adRequest.addTestDevice("DDE8F001C78F8B58CE003B1327202313");
以及在此修正案之后到现在为止显示的admob banner dosent!