我的应用程序有一个包含三个元素的底部菜单。如果单击它,则会出现相应的片段。当我尝试快速一个接一个地单击更多对象时,会出现问题。
这是Android Studio上的运行报告所说的:
java.lang.IllegalStateException: Restarter must be created only during owner's initialization stage
at androidx.savedstate.SavedStateRegistryController.performRestore(SavedStateRegistryController.java:58)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2585)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:838)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
这是我的底部菜单xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_nav"
android:layout_width="wrap_content"
android:layout_height="56dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/nav_bar_color"
app:itemTextColor="@color/nav_bar_color"
app:menu="@menu/nav_items">
</com.google.android.material.bottomnavigation.BottomNavigationView>
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/main_nav"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="0dp"
android:layout_marginTop="56dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp" /></RelativeLayout>
这是我的MainActivity.class(仅是片段/菜单的代码):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Other stuff....
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById((R.id.main_nav));
homeFragment = new HomeFragment();
filterFragment = new FilterFragment();
logsFragment = new LogsFragment();
setFragment(homeFragment);
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_home :
mMainNav.setItemBackgroundResource(R.color.colorPrimary);
setFragment(homeFragment);
return true;
case R.id.nav_filter:
mMainNav.setItemBackgroundResource(R.color.colorPrimary);
setFragment(filterFragment);
return true;
case R.id.nav_logs :
mMainNav.setItemBackgroundResource(R.color.colorPrimary);
setFragment(logsFragment);
return true;
default :
return false;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(new Intent(MainActivity.this, Setting.class));
return true;
}
return super.onOptionsItemSelected(item);
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_right_to_left, R.anim.exit_rigth_to_left, R.anim.enter_left_to_right, R.anim.exit_left_to_right);
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}}
最后,这是我的xml事务之一(enter_left_to_right.xml):
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%"
android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:duration="250"/></set>
我在互联网上进行搜索,但找不到任何有用的帮助吗?
(我的应用使用sdk 29编译,并支持26到29)