2017-01-05 24 views
0

我在我的应用程序崩溃或循环在我的Android应用程序之一,扩展活动,我使用onResume()刷新从PreferenceActivity返回活动,这种方式...如果我使用onResume()方法

public class ListViewWebShort extends Activity { 
    ListView listView; 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     this.onCreate(null); 
    } 
    .... 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list_view); 


     // Get ListView object from xml 
     listView = (ListView) findViewById(R.id.list); 
     .... 

..在这里,应用程序工作正常。

在另一应用程序,其中,I延伸AppCompatActivity,相同的onResume()方法,使我的应用程序崩溃或环上开始:

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 
    ListView listView; 
    ArrayAdapter<String> adapter; 
    .... 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     this.onCreate(null); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     .... 

如果使用requestWindowFeature(Window.FEATURE_NO_TITLE);以前的setContentView,我看到在调试该应用程序中的循环getDelegate()的setContentView(layoutResID),里面AppCompactActivity.java,但如果我删除它,在同一个函数的应用功能界别。 这真的让我疯狂......我错过了什么?

先感谢您的任何建议。

+0

为什么不使用没有操作栏的主题而不使用NoTitle? –

+0

@KarunShrestha,但我确实想要动作吧.. – gabolander

回答

1

不要直接调用任何活动的生命周期方法。这些方法旨在从系统中调用(控制反转)。

尤其是在超级实现的onCreate的()有可能发生的事情,导致崩溃,如果他们被称为在错误的顺序。

尝试在onCreate()和onResume()中分别调用onCreate()和onResume()方法调用该方法。

+0

这是有道理的,我会尝试这种方式,非常感谢你。但我不明白为什么在许多应用程序中,使用我所述的onResume()(以及某些开发人员在此建议的内容)可以正常工作,并且在某些情况下不会... – gabolander

+0

onResume()不是问题。但是你在你的onResume()方法中调用onCreate()方法。这是不允许的,因为onCreate()是一个生命周期方法。我无法相信任何严肃的开发人员会建议您在onResume()中调用onCreate()。 – Christopher

+0

是@Christopher,我真的认为你是对的。我在这里找到,例如(http://stackoverflow.com/questions/3053761/reload-activity-in-android)但在其他许多帖子中,谁使用我的旧(错误)方法的建议:-) 谢谢“唤醒我”LOL – gabolander

0

其实在的onCreate的每一个电话,应用程序的的onResume方法也被称为。

所以才打动你在OnCreate来写的onResume任何代码。

希望它有帮助。

+0

因此,首先我尝试按@Christopher的建议创建一个单独的方法,并且我只能从onResume()调用'it',对吗?我会尝试这种方式,谢谢。 – gabolander

+0

我认为这是要走的路。 – Christopher

+0

它的工作原理就像这样:-) 感谢@Christopher和Minasploit – gabolander

相关问题