我在Android 8.1上遇到了摩托罗拉Moto G(5S)(蒙大拿州)的吼叫异常。
我该怎么做才能找到问题根源?
我无法以一致的方式重现崩溃,但给人的印象是android试图添加已经添加到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)
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);
}
}
}
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);
}
}