我正在尝试在我的Android应用程序中实现广告。当我将广告添加到我的xml布局时,我怀疑内存泄漏,所以我尝试了这篇文章中描述的方法: - Admob Memory Leak - avoiding by using empty activity
广告已呈现并且没有问题,除非在我的应用程序的屏幕之间,如果我意外地点击设备的主页按钮,然后如果我转到最近的应用程序并选择我的,我得到错误: - < / p>
03-22 22:17:56.604: E/AndroidRuntime(27206): Caused by: java.lang.IllegalStateException: This activity should be created only once during the entire application life
03-22 22:17:56.604: E/AndroidRuntime(27206): at com.xyz.watch.AdMobActivity.<init>(AdMobActivity.java:16)
03-22 22:17:56.604: E/AndroidRuntime(27206): at java.lang.Class.newInstanceImpl(Native Method)
03-22 22:17:56.604: E/AndroidRuntime(27206): at java.lang.Class.newInstance(Class.java:1409)
03-22 22:17:56.604: E/AndroidRuntime(27206): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-22 22:17:56.604: E/AndroidRuntime(27206): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
03-22 22:17:56.604: E/AndroidRuntime(27206): ... 11 more
03-22 22:17:56.684: W/System.err(27206): java.io.FileNotFoundException: /data/plog.log (Permission denied)
我的AdMobActivity的启动模式是singleInstance,所以也许这就是原因。我该怎么做才能发挥作用?
更新: - 我的代码
在我的首次发布活动(MainActivity)中
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatabaseAdapter databaseAdapter = new DatabaseAdapter(
getApplicationContext());
databaseAdapter.open();
databaseAdapter.close();
}
@Override
protected void onResume() {
super.onResume();
if (AdMobActivity.AdMobMemoryLeakWorkAroundActivity == null) {
Log.i("CHAT", "starting the AdMobActivity");
AdMobActivity.startAdMobActivity(this);
}
}
AdMobActivity: -
public final class AdMobActivity extends Activity {
public static AdMobActivity AdMobMemoryLeakWorkAroundActivity;
public AdMobActivity() {
super();
if (AdMobMemoryLeakWorkAroundActivity != null) {
throw new IllegalStateException("This activity should be created only once during the entire application life");
}
AdMobMemoryLeakWorkAroundActivity = this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("CHAT", "in onCreate - AdMobActivity");
finish();
}
public static final void startAdMobActivity(Activity activity) {
Log.i("CHAT", "in startAdMobActivity");
Intent i = new Intent();
i.setComponent(new ComponentName(activity.getApplicationContext(), AdMobActivity.class));
activity.startActivity(i);
}
}
行号16是
throw new IllegalStateException("This activity should be created only once during the entire application life");
答案 0 :(得分:0)
请查看此代码。我只用过这个。我没有遇到任何内存泄漏。
http://jmsliu.com/209/add-google-admob-in-android-application.html
希望这会对你有所帮助。也尝试使用此代码。这不会集成在xml布局中。只是按照语法进行。
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import com.google.ads.*;
public class AdmobExample extends Activity {
/** Called when the activity is first created. */
private AdView myAdView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myAdView = new AdView(this, AdSize.BANNER, "youradmob id. ");
//get layoutView
LinearLayout rootView = (LinearLayout)this.findViewById(R.id.rootViewGroup);
LinearLayout.LayoutParams layoutParams = new LayoutParams(480, 75);
rootView.addView(myAdView, 0, layoutParams);
AdRequest re = new AdRequest();
re.setGender(AdRequest.Gender.FEMALE);
//re.setTestDevices(testDevices);
//re.setTesting(testing)
myAdView.loadAd(re);
}
}