android – OnCreate方法不断重复调用

android – OnCreate方法不断重复调用,第1张

概述更新:感谢大家试图帮助我解决这个bug.我仍然不确定事情,我能够回滚到以前的承诺,并从那里继续发展.这个以前的提交确实显示了同样的错误,但是在我注释掉了button.performClick()之后,它已经消失了.奇怪的是,这并不适用于最近的提交. 我仍然不明白这个错误,并希望能帮助确定根本原因.我最大的恐惧是不经意地重新介绍它. 我曾经见过最疯狂的错误. OnCreate方法被一再调用,冻结了我 更新:感谢大家试图帮助我解决这个BUG.我仍然不确定事情,我能够回滚到以前的承诺,并从那里继续发展.这个以前的提交确实显示了同样的错误,但是在我注释掉了button.performClick()之后,它已经消失了.奇怪的是,这并不适用于最近的提交.

我仍然不明白这个错误,并希望能帮助确定根本原因.我最大的恐惧是不经意地重新介绍它.

我曾经见过最疯狂的错误.

OnCreate方法被一再调用,冻结了我的应用程序,并给了我轻微的闪烁.然后,唯一的解决方案是退出到主屏幕,强制从设置菜单退出应用程序.

这是正在发生的细节:

>申请开始(主要活动)
>主要活动调用第二个活动
> Second Activity调用onCreate,设置正常
>第二个活动随机决定退出创建 – 我认为这是发生了什么
> Second Activity的onCreate再次被调用.它不会回到主要活动.

我运行了一个调试器,看来第二个活动成功完成了onComplete / onResume序列,然后决定退出并重启.

有没有人听说过这个行为?

我没有注意到抛出任何异常.此外,在调试过程中,我确实已经进行了检查,您所看到的那些位置是无声的. (这是较早的代码,然后我用print语句)

更新:当尝试停止该过程时,我必须打开飞行模式.这意味着它与这个代码块有关(Second Activity)

else if (Network.haveNetworkConnection(Login.getContext()) && Login.checkClIEntID())        {...}

没有互联网,它将打到else语句,不显示此行为.

码:

主要活动的onResume(),我称之为第二个活动:

@OverrIDe    public voID onResume()    {        super.onResume();        //Check If logged in,else go to login page        Login.setContext(getApplicationContext());        //reset Notification Number        GCMIntentService.cancelNotifications();        /** GO TO LOGIN **/        if(!Login.isLoggedIn())        {            //If user is not logged in,open login page            System.out.println("RESUMING MAIN AND STARTING LOGIN INTENT");            Intent intent = new Intent(ActivityMain.this,activitylogin.class);            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);            startActivity(intent);        } else        {            Login.setupStuffOnce();            Event.pullEvents(); //Get New Events            //Update ListVIEw            updateMainFeed();        }    }

这是第二个活动:

public class activitylogin extends Activity{    private String postData;    //private Context c;    //final Timer timer = new Timer();    //Facebook Stuff    private Facebook facebook = new Facebook(Config.FBAPPID);    private AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);    //Layout Stuff    EditText username,password;    button loginbutton,signupbutton;    @OverrIDe    public voID onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        // Open Database        Login.setContext(getApplicationContext());        Database.open(getApplicationContext());    }    /*     * @OverrIDe public voID onPause() { s }     */    @OverrIDe    public voID onResume()    {        super.onResume();        // shouldn't put here but oh well        init();        //If coming from ActivitySignup        if(Transfer.username != null)        {            username.setText(Transfer.username);            password.setText(Transfer.password);            Transfer.password = null;            Transfer.username = null;            loginbutton.performClick();        }    }    public voID init()    {        Login.getUserLoggedIn();        if (Login.isLoggedIn())        {            //Do Any Additional Setup            Login.setupStuffOnce();            // If user is logged in,open main            Intent intent = new Intent(activitylogin.this,ActivityMain.class);            //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);            startActivity(intent);        } else if (Network.haveNetworkConnection(Login.getContext()) && Login.checkClIEntID())        {            // Else,Make User Login            // Inflate Login and Present Website            String clIEntID = Login.getClIEntID();            System.out.println("clIEntID:" + clIEntID);            //System.exit(0);            postData =  "mobile=1&clIEnt_ID="+Login.getClIEntID();            // Inflate the vIEw            setContentVIEw(R.layout.activitylogin3);            username = (EditText) findVIEwByID(R.ID.username);            password = (EditText) findVIEwByID(R.ID.password);            //Inflate the button            loginbutton = (button) findVIEwByID(R.ID.loginbutton);            signupbutton = (button) findVIEwByID(R.ID.signupbutton);            signupbutton.setonClickListener(new VIEw.OnClickListener()            {                @OverrIDe                public voID onClick(VIEw v)                {                    Intent intent = new Intent(activitylogin.this,ActivitySignup.class);                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_top|Intent.FLAG_ACTIVITY_SINGLE_top);                    startActivity(intent);                }            });            loginbutton.setonClickListener(new VIEw.OnClickListener() {                  public voID onClick(VIEw vIEw) {                     int res = Login.sendLogin(username.getText().toString(),password.getText().toString());                     if(res == 202)                     {                         //Login Successful                         //Check if facebooked.                         if(Login.isFacebooked())                         {                             //Just go to main                             Intent intent = new Intent(activitylogin.this,ActivityMain.class);                             //Are these flags necessary?                             //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);                             startActivity(intent);                         } else                         {                             //Go to facebook login page                             //Intent intent = new Intent(activitylogin.this,ActivityFBLogin.class);                             //startActivity(intent);                             //Login via Facebook                             doFacebook();                         }                     } else                     {                         System.out.println("Login Failed: "+res);                         if(res == 405)                         {                             Toast.makeText(getApplicationContext(),"Incorrect Username/Password",Toast.LENGTH_SHORT).show();                             password.setText("");                         }                         else                             Toast.makeText(getApplicationContext(),"Network Error",Toast.LENGTH_SHORT).show(); //Not entirely true in all cases i think                     }                      /*Login.getUserLoggedIn();                        if(Login.isLoggedIn())                        {                            Intent intent = new Intent(activitylogin.this,ActivityMain.class);                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);                            startActivity(intent);                        } else {                            Toast.makeText(getApplicationContext(),"Please Login Above",Toast.LENGTH_SHORT).show();                        }*/                  }                });        } else        {            // Not Logged In and No Internet Access            setContentVIEw(R.layout.activitylogintext);            EditText text = (EditText) findVIEwByID(R.ID.text);            text.setText("No Internet Connection Detected\n requires internet to login");            button button = (button) findVIEwByID(R.ID.refreshbutton);            button.setonClickListener(new VIEw.OnClickListener() {                  public voID onClick(VIEw vIEw) {                        //Login.getUserLoggedIn();                        if(Network.haveNetworkConnection(Login.getContext()))                        {                            Intent intent = new Intent(activitylogin.this,activitylogin.class);                            //intent.setFlags();                            startActivity(intent);                        } else {                            Toast.makeText(getApplicationContext(),"No Internet Access Detected",Toast.LENGTH_SHORT).show();                        }                  }                });        }    }    @OverrIDe    public voID onActivityResult(int requestCode,int resultCode,Intent data) {        super.onActivityResult(requestCode,resultCode,data);        facebook.authorizeCallback(requestCode,data);    }    public voID doFacebook()    {         facebook.authorize(this,Config.facebookPermissions,new DialogListener() {                @OverrIDe                public voID onComplete(Bundle values) {                     /*SharedPreferences.Editor editor = state.edit();                     editor.putString("access_token",facebook.getAccesstoken());                     editor.putLong("access_expires",facebook.getAccessExpires());                     editor.commit();                        */                     //input into database                     Login.saveAccesstoken(facebook.getAccesstoken());                     Login.setFB(facebook.getAccesstoken());                     //Login.sendAccesstoken(facebook.getAccesstoken());                     //Intent into Main Activity                     Intent intent = new Intent(activitylogin.this,ActivityMain.class);                    //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);                    startActivity(intent);                }                @OverrIDe                public voID onFacebookError(FacebookError error) {                    Toast.makeText(getApplicationContext(),"Error: "+error.getErrorType(),Toast.LENGTH_SHORT).show();                }                @OverrIDe                public voID onError(DialogError e) {                    Toast.makeText(getApplicationContext(),"Error: "+e.getMessage(),Toast.LENGTH_SHORT).show();                }                @OverrIDe                public voID onCancel() {}            });    }    public boolean checkForUserID(Context c)    {        try{            String res = Network.getUrl("www.website.com/mobile.PHP?got_user=1&clIEnt_ID="+Login.getClIEntID());            JsONObject Json = JsON.constructObject(res);            if(JsON.handleCode(Json))            {                if(Json.getString("type").equals("userID"))                {                    Login.setLogin(Json.getString("data"));                    return true;                }            }        } catch(Exception e)        {            //Silent Fail        }        return false;    }}
解决方法 我相信如果您在调用SecondActivity之后完成了MainActivity,该问题将得到解决.问题可能是在恢复MainActivity时onResume事件被立即触发.那是因为MainActivity可能在后台被破坏并重新创建.另一个解决方案是使用onSaveInstanceState保存Activity的状态.有关更多信息,请参阅 here. 总结

以上是内存溢出为你收集整理的android – OnCreate方法不断重复调用全部内容,希望文章能够帮你解决android – OnCreate方法不断重复调用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1130888.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存