2

I have an app that has a navigation bar with multiple fragments.

Looking at the google play console, and looking at the crashes. I see a bunch of java.lang.IllegalArgumentExcpetion

I cannot seem to replicate the problems, and in the logs nothing points to my code.

Can anybody decipher what could be the problem?

Most of the errors look like this while some have different line numbers.

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)

EDIT: Below is the MainActivity onNavigationItemSelected function:

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;

}

When i'm inside a fragment and want to goto another, this is the code I use to do that:

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

When inside a activity here is the code:

// 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();
Derek
  • 2,389
  • 1
  • 16
  • 28
  • Please paste the code where you replace fragments – fdelafuente Jun 12 '17 at 17:40
  • @fn5341 I edited to show my main activity, There are also other places that replace the fragment, example when i'm in new transaction fragment, I can go to the pos fragment – Derek Jun 12 '17 at 17:46
  • Check out this [question](https://stackoverflow.com/questions/25665642/java-lang-illegalargumentexception-no-view-found-for-id-0x1020002-androidid-c) – fdelafuente Jun 12 '17 at 21:02
  • Do you have any fragments in other activities? – Tony Jun 20 '17 at 22:46

2 Answers2

2

What is happening is your calling a fragment from a different activity

You're trying: Activity2 -> accountSettingsFragment which it can't actually see.

What you need to do is goto the mainActivity that holds the accountSettingsFragment

Intent i = new Intent(currentActivity.this,MainActivity.class);
startActivity(i);
Tony
  • 2,587
  • 1
  • 21
  • 30
1

I have solved this problem, as describe by @OmerHayat answered here on my Question. Visit this Link Here

Also Put this into the Try catch block

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();