【问题标题】:Activity Does not launch from the recent activities:- Android活动不从最近的活动启动:- Android
【发布时间】:2013-03-13 02:46:17
【问题描述】:

我正在尝试在我的 android 应用程序中实现广告。当我在我的 xml 布局中添加广告时,我怀疑内存泄漏,所以我尝试了这篇文章中描述的方法:-Admob Memory Leak - avoiding by using empty activity

广告已呈现并且没有问题,除了如果在我的应用程序屏幕之间,如果我不小心点击了设备的主页按钮,然后如果我转到最近的应用程序并选择我的应用程序,我会收到错误:-

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);
    }
}

AdMob 活动:-

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");

【问题讨论】:

  • 您是否尝试过在 onResume 方法中移动 Admob 相关代码?
  • 不..让我尝试这样做..
  • 不...还是同样的错误
  • 我会把我的代码贴在这里...
  • @TacB0sS 请帮忙.. 我认为这是您最初的解决方案,所以您也许可以提供帮助

标签: android admob illegalstateexception


【解决方案1】:

请看一下这段代码。我只用过这个。我没有遇到任何内存泄漏。

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);
    }
}

【讨论】:

  • 嘿,谢谢.. 我会试试这个。在此示例中,您将在请求中发送性别信息。你是怎么做到的.. 是否可以获取用户的性别信息?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多