调用Android IllegalArgumentException

时间:2017-06-12 17:35:28

标签: java android

我的应用程序有一个包含多个片段的导航栏。

查看谷歌播放控制台,并查看崩溃。我看到一堆java.lang.IllegalArgumentExcpetion

我似乎无法复制问题,在日志中没有任何内容指向我的代码。

任何人都可以破译可能出现的问题吗?

大多数错误看起来像这样,而有些则有不同的行号。

java.lang.IllegalArgumentException: 
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1293)
  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
  at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
  at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
  at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:158)
  at android.app.ActivityThread.main(ActivityThread.java:7225)
  at java.lang.reflect.Method.invoke(Native Method:0)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

修改 以下是MainActivity onNavigationItemSelected函数:

switch (id) {

    case R.id.dashboard:
        if (currentFragment instanceof DashboardFragment) break;
        // SET DASHBOARD FRAGMENT
        DashboardFragment fragment = new DashboardFragment();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
        break;
    case R.id.new_trans:

        // CLEAR
        staticTransaction.reset();
        staticCart.reset();

        if (currentFragment instanceof newTransFragment) break;
        // SET NEW TRANS FRAGMENT
        newTransFragment newTransFragment = new newTransFragment();
        fragmentTransaction.replace(R.id.fragment_container, newTransFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        break;
    case R.id.trans_history:
        if (currentFragment instanceof transHistoryFragment) break;
        // SET TRANS HISTORY FRAGMENT
        transHistoryFragment transHistoryFragment = new transHistoryFragment();
        fragmentTransaction.replace(R.id.fragment_container, transHistoryFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        break;
    case R.id.orders:
        if (currentFragment instanceof OrdersFragment) break;
        // SET ORDERS FRAGMENT
        OrdersFragment ordersFragment = new OrdersFragment();
        fragmentTransaction.replace(R.id.fragment_container, ordersFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        break;
    case R.id.pos:

        // CLEAR
        staticTransaction.reset();
        staticCart.reset();

        if (currentFragment instanceof POSFragment) break;
        // SET POS FRAGMENT
        POSFragment posFragment = new POSFragment();
        fragmentTransaction.replace(R.id.fragment_container, posFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        break;
    case R.id.products_list:
        if (currentFragment instanceof ProductsFragment) break;
        // SET PRODUCTS FRAGMENT
        ProductsFragment productsFragment = new ProductsFragment();
        fragmentTransaction.replace(R.id.fragment_container, productsFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        break;
    case R.id.customers_list:
        if (currentFragment instanceof CustomersFragment) break;
        // SET CUSTOMERS FRAGMENT
        CustomersFragment customersFragment = new CustomersFragment();
        fragmentTransaction.replace(R.id.fragment_container, customersFragment);
        fragmentTransaction.commit();
        break;
    case R.id.account_settings:
        if (currentFragment instanceof accountSettingsFragment) break;
        // SET ACCOUNT SETTINGS FRAGMENT
        accountSettingsFragment accountSettingsFragment = new accountSettingsFragment();
        fragmentTransaction.replace(R.id.fragment_container, accountSettingsFragment);
        fragmentTransaction.commit();
        break;
    case R.id.settings:
        if (currentFragment instanceof settingsFragment) break;
        // SET SETTINGS FRAGMENT
        settingsFragment settingsFragment = new settingsFragment();
        fragmentTransaction.replace(R.id.fragment_container, settingsFragment);
        fragmentTransaction.commit();
        break;
    case R.id.test_connection:
        if (currentFragment instanceof testConnectionFragment) break;
        // SET TEST CONNECTION FRAGMENT
        testConnectionFragment testConnectionFragment = new testConnectionFragment();
        fragmentTransaction.replace(R.id.fragment_container, testConnectionFragment);
        fragmentTransaction.commit();
        break;
    case R.id.sync:
        if (currentFragment instanceof SyncFragment) break;
        SyncFragment syncFragment = new SyncFragment();
        fragmentTransaction.replace(R.id.fragment_container, syncFragment);
        fragmentTransaction.commit();
        break;

}

当我在一个片段里面想要转到另一个片段时,这就是我用来做这个的代码:

// GOTO POS
POSFragment posFragment = new POSFragment();
getFragmentManager().beginTransaction().replace(R.id.fragment_container, posFragment).addToBackStack(null).commit();

这里的活动内容是代码:

// GOTO ACCOUNT SETTINGS
accountSettingsFragment fragment = new accountSettingsFragment();
FragmentTransaction fragmentTransaction = CustomerViewActivity.this.getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
Bundle bundle = new Bundle();
bundle.putInt("timedOut", 1);
fragment.setArguments(bundle);
fragmentTransaction.commit();

2 个答案:

答案 0 :(得分:1)

发生的事情是你从另一个活动中调用一个片段

您正在尝试:Activity2 -> accountSettingsFragment它实际上无法看到。

您需要做的是转到包含accountSettingsFragment

的mainActivity
Intent i = new Intent(currentActivity.this,MainActivity.class);
startActivity(i);

答案 1 :(得分:1)

我已经解决了这个问题,正如@OmerHayat在我的问题上所解释的那样。访问此链接Here

同时将其放入Try catch块

accountSettingsFragment fragment = new accountSettingsFragment();
FragmentTransaction fragmentTransaction = CustomerViewActivity.this.getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
Bundle bundle = new Bundle();
bundle.putInt("timedOut", 1);
fragment.setArguments(bundle);
fragmentTransaction.commit();