活动不从最近的活动推出: - 安卓 [英] Activity Does not launch from the recent activities:- Android

查看:125
本文介绍了活动不从最近的活动推出: - 安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我的Andr​​oid应用程序的广告。我怀疑是内存泄漏,当我加入广告到我的XML布局,所以我试图在这篇文章中介绍的方法: - <一个href="http://stackoverflow.com/questions/9558708/admob-memory-leak-avoiding-by-using-empty-activity">Admob内存泄漏 - 避免使用空活动

该广告的呈现并有不同之处在于,如果在我的应用程序的屏幕之间,如果我在设备的主页按钮点击意外,然后如果我去最近的应用程序,然后选择我的,我得到的错误没有问题: -

  03-22 22:17:56.604:E / AndroidRuntime(27206):java.lang.IllegalStateException:产生的原因这个活动应该在整个应用程序生命周期内只创建一次
。03-22 22:17:56.604:E / AndroidRuntime(27206):在com.xyz.watch.AdMobActivity&LT; INIT&GT;(AdMobActivity.java:16)
03-22 22:17:56.604:E / AndroidRuntime(27206):在java.lang.Class.newInstanceImpl(本机方法)
03-22 22:17:56.604:E / AndroidRuntime(27206):在java.lang.Class.newInstance(Class.java:1409)
03-22 22:17:56.604:E / AndroidRuntime(27206):在android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-22 22:17:56.604:E / AndroidRuntime(27206):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
03-22 22:17:56.604:E / AndroidRuntime(27206):11 ...更多
03-22 22:17:56.684:W / System.err的(27206):java.io.FileNotFoundException:/data/plog.log(权限被拒绝)
 

我AdMobActivity的启动模式是singleInstance所以也许这就是原因。我应该怎么做这个工作?

更新 - 我的code

在我的第一次发射活动(MainActivity)

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    DatabaseAdapter databaseAdapter =新DatabaseAdapter(
            getApplicationContext());
    databaseAdapter.open();
    databaseAdapter.close();
}

    @覆盖
保护无效onResume(){
    super.onResume();

    如果(AdMobActivity.AdMobMemoryLeakWorkAroundActivity == NULL){
        Log.i(聊天,启动AdMobActivity);
        AdMobActivity.startAdMobActivity(本);
    }
}
 

AdMobActivity: -

 公开最后一类AdMobActivity延伸活动{

公共静态AdMobActivity AdMobMemoryLeakWorkAroundActivity;

公共AdMobActivity(){
    超();
    如果(AdMobMemoryLeakWorkAroundActivity!= NULL){
        抛出新IllegalStateException异常(本次活动应在整个应用生命周期内只创建一次);
    }
    AdMobMemoryLeakWorkAroundActivity =这一点;
}

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    Log.i(中的onCreate  -  AdMobActivity,聊天,);
    完();
}

公共静态最终无效startAdMobActivity(活动活动){
    Log.i(在startAdMobActivity,聊天,);
    意图I =新意图();
    i.setComponent(新单元名(activity.getApplicationContext(),AdMobActivity.class));
    activity.startActivity(ⅰ);
}
 

}

行没有。 16

 抛出新IllegalStateException异常(本次活动应在整个应用生命周期内只创建一次);
 

解决方案

请看看这个code。我已经使用这个唯一的。我没有遇到任何内存泄漏。

http://jmsliu.com/209/add-谷歌,AdMob的功能于Android的application.html

希望这会帮助你。

与此code也一试。这并不在XML布局整合。只是做亲语法。

 进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.LinearLayout;
进口android.widget.LinearLayout.LayoutParams;

进口com.google.ads *。

公共类AdmobExample延伸活动{
    / **第一次创建活动时调用。 * /
    私人AD浏览报myAdView;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        myAdView =新的AD浏览报(这一点,AdSize.BANNER,youradmob ID。);

        //获取layoutView
        的LinearLayout rootView =(的LinearLayout)this.findViewById(R.id.rootViewGroup);
        LinearLayout.LayoutParams的LayoutParams =新的LayoutParams(480,75);

        rootView.addView(myAdView,0,的LayoutParams);

        AdRequest重=新AdRequest();
        re.setGender(AdRequest.Gender.FEMALE);
        //re.setTestDevices(testDevices);
        //re.setTesting(testing)

        myAdView.loadAd(重);
    }
}
 

I am trying to implement ads in my android application. I suspected memory leaks when i added ads to my xml layout so i tried the approach described in this post :- Admob Memory Leak - avoiding by using empty activity

The ads are rendered and there is no problem except that if in between my app's screens, if i click on home button of the device accidently and then if i go to recent applications and select mine, I get the error :-

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)

The launch mode of my AdMobActivity is singleInstance so maybe thats the reason. What should i do for this to work?

Update :- My code

Inside my first launch activity (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);
}

}

Line no. 16 is

 throw new IllegalStateException("This activity should be created only once during the entire application life");

解决方案

Please have a look at this code. i have used this only. i didnt faced any memory leaks.

http://jmsliu.com/209/add-google-admob-in-android-application.html

hope this will help you.

have a try with this code also. this doesn't to integrate in xml layout. just do it pro grammatically.

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

这篇关于活动不从最近的活动推出: - 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆