RuntimeException:带有MainActivity.onStart的java.lang.IllegalArgumentException

时间:2017-09-18 12:41:08

标签: android performance android-layout android-studio android-fragments

可能与

java.lang.IllegalArgumentException when switching fragments 
重复但是问题的情况与此链接不同。

这是我的代码

public class MainActivity extends AppCompatActivity{         
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main_layout);
    bindWidgetsWithAnEvent();
 } 
private void bindWidgetsWithAnEvent() {
    tabsLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(final TabLayout.Tab tab) {
            setCurrentTabFragment(tab.getPosition());
            Log.d(TAG, "Position " + tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            Log.d(TAG, String.valueOf(tab.getPosition()));
        }
    });
}
public void setCurrentTabFragment(int tabPosition) {
    switch (tabPosition) {
        case 0:
            replaceFragment(fragmentName);
            break;
        case 1:
            replaceFragment(fragmentName);
            break;
        default:
            break;
    }
}
public void replaceFragment(Fragment fragment) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.flContent, fragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}
@Override
protected void onStart() {
    super.onStart();
}
}

以下是Google控制台捕获的堆栈跟踪,搜索后仍未获得解决方案。如果有人有更好的解决方案或遇到同样的问题,请告诉我。

java.lang.IllegalStateException:
  at android.support.v4.app.FragmentManagerImpl.checkStateLoss (FragmentManager.java:1842)
  at android.support.v4.app.FragmentManagerImpl.enqueueAction (FragmentManager.java:1860)
  at android.support.v4.app.BackStackRecord.commitInternal (BackStackRecord.java:650)
  at android.support.v4.app.BackStackRecord.commit (BackStackRecord.java:609)
  at com.safarifone.waafi.ui.activities.MainActivity.replaceFragment (MainActivity.java:354)
  at com.safarifone.waafi.ui.activities.MainActivity.setCurrentTabFragment (MainActivity.java:336)
  at com.safarifone.waafi.ui.activities.MainActivity$3.onTabSelected (MainActivity.java:303)
  at android.support.design.widget.TabLayout.dispatchTabSelected (TabLayout.java:1164)
  at android.support.design.widget.TabLayout.selectTab (TabLayout.java:1157)
  at android.support.design.widget.TabLayout.selectTab (TabLayout.java:1127)
  at android.support.design.widget.TabLayout$Tab.select (TabLayout.java:1426)
  at android.support.design.widget.TabLayout$TabView.performClick (TabLayout.java:1536)
  at android.view.View$PerformClick.run (View.java:20300)
  at android.os.Handler.handleCallback (Handler.java:815)
  at android.os.Handler.dispatchMessage (Handler.java:104)
  at android.os.Looper.loop (Looper.java:210)
  at android.app.ActivityThread.main (ActivityThread.java:5833)
  at java.lang.reflect.Method.invoke (Native Method)
  at java.lang.reflect.Method.invoke (Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1113)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:879)

帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

基本上当你替换片段时它会丢失它的状态,这是Android OS中非常了解的错误,并且大多数情况发生在你的活动处于暂停状态时。 我面临类似的问题,我通过引入一个bollean变量并在catch块中将其设置为true来解决这个问题。您可以重现此错误,只需添加10到15秒的帖子,同时提交片段并运行您的活动并在10 15秒后将其置于暂停状态,当活动处于暂停状态时尝试提​​交片段时您可以发现崩溃。

public void replaceFragment(Fragment fragment) {
try {
   FragmentManager fm = getSupportFragmentManager();
   FragmentTransaction ft = fm.beginTransaction();
   ft.replace(R.id.flContent, fragment);
   ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
   ft.commit();
  }catch (Exception e)
            {
                stateSaveFragment = true;
                e.printStackTrace();
            }
 }

在活动的后续恢复方法中,替换你的片段并再次将stateSaveFragment设置为false。我解决这个问题,我帮助你。

@Override
protected void onPostResume() {
    super.onPostResume();
    if(stateSaveFragment){
        replaceFragment(fragmentName);
    }
    stateSaveFragment=false;
}