java.lang.IllegalStateException во время ViewPager.addView - PullRequest
0 голосов
/ 29 января 2019

Я сталкиваюсь с этим ниже исключением на Motorola Moto G (5S) (Монтана), Android 8.1.

Что я могу сделать, чтобы обнаружить корень проблемы?

Я не могувоспроизвести сбой согласованным способом, но у меня сложилось впечатление, что андроид пытается добавить фрагмент, уже добавленный в viewpager.

Я застрял с этой проблемой по дням, пожалуйста, помогите мне.

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2861)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2943)
  at android.app.ActivityThread.-wrap11 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1630)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6626)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:811)
Caused by: java.lang.IllegalStateException: 
  at android.view.ViewGroup.addViewInner (ViewGroup.java:4941)
  at android.view.ViewGroup.addView (ViewGroup.java:4772)
  at android.support.v4.view.ViewPager.addView (ViewPager.java:1485)
  at android.view.ViewGroup.addView (ViewGroup.java:4712)
  at android.view.ViewGroup.addView (ViewGroup.java:4685)
  at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1466)
  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState (FragmentManager.java:1784)
  at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1852)
  at android.support.v4.app.FragmentManagerImpl.dispatchStateChange (FragmentManager.java:3269)
  at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated (FragmentManager.java:3229)
  at android.support.v4.app.FragmentController.dispatchActivityCreated (FragmentController.java:201)
  at android.support.v4.app.FragmentActivity.onStart (FragmentActivity.java:620)
  at android.support.v7.app.AppCompatActivity.onStart (AppCompatActivity.java:178)
  at android.app.Instrumentation.callActivityOnStart (Instrumentation.java:1356)
  at android.app.Activity.performStart (Activity.java:7052)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2824)

CoreActivity.com

public class CoreActivity extends AppCompatActivity {

    ActionBar actionBar;
    ActivityCoreBinding b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        b = DataBindingUtil.setContentView(this, R.layout.activity_core);


        configViewPager();
        configNavigation();
        configToobar();
    }

    private void configViewPager() {
        CoreActivityAdapter adapter = new CoreActivityAdapter(getSupportFragmentManager());
        adapter.addFragment(new CarteiraFragment(), "Carteira");
        adapter.addFragment(new ProfileFragment(), "Perfil");

        b.viewpager.setAdapter(adapter);
        b.viewpager.setOffscreenPageLimit(3);

    }

    private void configNavigation() {
        b.navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.carteira:
                        b.viewpager.setCurrentItem(0);
                        break;
                    case R.id.perfil:
                        b.viewpager.setCurrentItem(3);
                        break;
                }
                return true;
            }
        });
    }

    private void configToobar() {
        setSupportActionBar(b.toolbar);
        actionBar = getSupportActionBar();
        actionBar.setTitle(R.string.carteira);
    }

    @Override
    public void onBackPressed() {
        if (b.viewpager.getCurrentItem() == 0) {
            super.onBackPressed();
        } else {
            b.viewpager.setCurrentItem(0);
        }
    }
}

CoreActivityAdapter.java

public class CoreActivityAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public CoreActivityAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }


    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
...