title: java.lang.ClassCastException: MainActivity cannot be cast?
body: <p>Here's the full log </p>
  
  <pre><code>11-16 04:13:35.810 3555-3555/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.steven.test, PID: 3555
                                                   java.lang.ClassCastException: com.steven.test.MainActivity cannot be cast to com.steven.test.providers.priceticker.currencylist.AllCurrencyListFragment$FavoritesListUpdater
                                                       at com.steven.test.providers.priceticker.currencylist.AllCurrencyListFragment.onAttach(AllCurrencyListFragment.java:182)
                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1372)
                                                       at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1188)
                                                       at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1071)
                                                       at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:115)
                                                       at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2380)
                                                       at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
                                                       at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2215)
                                                       at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:649)
                                                       at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)
                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1238)
                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
                                                       at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1616)
                                                       at android.view.View.measure(View.java:22091)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
                                                       at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:739)
                                                       at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:91)
                                                       at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1361)
                                                       at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:809)
                                                       at android.view.View.measure(View.java:22091)
                                                       at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1632)
                                                       at com.duolingo.open.rtlviewpager.RtlViewPager.onMeasure(RtlViewPager.java:211)
                                                       at android.view.View.measure(View.java:22091)
                                                       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                       at android.view.View.measure(View.java:22091)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
                                                       at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:739)
                                                       at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:91)
                                                       at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1361)
                                                       at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:809)
                                                       at android.view.View.measure(View.java:22091)
                                                       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                       at android.view.View.measure(View.java:22091)
                                                       at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1059)
                                                       at android.view.View.measure(View.java:22091)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                       at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
                                                       at android.view.View.measure(View.java:22091)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                       at android.view.View.measure(View.java:22091)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                       at android.view.View.measure(View.java:22091)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
  11-16 04:13:35.811 3555-3555/? E/AndroidRuntime:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
                                                       at android.view.View.measure(View.java:22091)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                       at com.android.internal.policy.DecorView.onMeasure(DecorView.java:725)
                                                       at android.view.View.measure(View.java:22091)
                                                       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2447)
                                                       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1529)
                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1786)
                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1417)
                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6934)
                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:979)
                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:791)
                                                       at android.view.Choreographer.doFrame(Choreographer.java:726)
                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:965)
                                                       at android.os.Handler.handleCallback(Handler.java:790)
                                                       at android.os.Handler.dispatchMessage(Handler.java:99)
                                                       at android.os.Looper.loop(Looper.java:164)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6578)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:460)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
  </code></pre>
  
  <p>Here are my activities :</p>
  
  <p>The fragment which starts and passes to the other fragment -</p>
  
  <pre><code>public class CurrencyListTabsFragment extends Fragment implements ViewPager.OnPageChangeListener,
          FavoriteCurrencyListFragment.AllCoinsListUpdater, AllCurrencyListFragment.FavoritesListUpdater {
  
      private SectionsPagerAdapterCurrencyList mSectionsPagerAdapter;
      private View rootView;
      public ViewPager mViewPager;
      public static String baseImageURL = "";
      public static String SYMBOL = "SYMBOL";
      private Toolbar mToolbar;
      public final static String DAY = "24h";
      public final static String WEEK = "7d";
      public final static String HOUR = "1h";
      public final static String SORT_SETTING = "sort_setting";
      public Fragment context;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          rootView = inflater.inflate(R.layout.activity_currency_list_tabs, container, false);
          context = this;
          mToolbar = rootView.findViewById(R.id.toolbar_currency_list);
          ((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);
          TabLayout tabLayout = rootView.findViewById(R.id.currency_list_tabs);
          mViewPager = rootView.findViewById(R.id.currency_list_tabs_container);
  
          mSectionsPagerAdapter = new SectionsPagerAdapterCurrencyList(getActivity().getSupportFragmentManager());
          mViewPager.setAdapter(mSectionsPagerAdapter);
          mViewPager.setOffscreenPageLimit(2);
          mViewPager.addOnPageChangeListener(this);
  
          tabLayout.setupWithViewPager(mViewPager);
          tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
  
          return rootView;
      }
  
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  
      }
  
      @Override
      public void onPageSelected(int position) {
  
          Fragment fragment = mSectionsPagerAdapter.getFragment(position);
          if (fragment != null) {
              fragment.onResume();
          }
      }
  
      @Override
      public void onPageScrollStateChanged(int state) {
  
      }
  
      public void removeFavorite(CMCCoin coin) {
          FavoriteCurrencyListFragment frag = (FavoriteCurrencyListFragment) mSectionsPagerAdapter.getFragment(1);
          if (frag != null) {
              frag.removeFavorite(coin);
          }
      }
  
      public void addFavorite(CMCCoin coin) {
          FavoriteCurrencyListFragment frag = (FavoriteCurrencyListFragment) mSectionsPagerAdapter.getFragment(1);
          if (frag != null) {
              frag.addFavorite(coin);
          }
      }
  
      public void allCoinsModifyFavorites(CMCCoin coin) {
          AllCurrencyListFragment frag = (AllCurrencyListFragment) mSectionsPagerAdapter.getFragment(0);
          if (frag != null) {
              frag.getAdapter().notifyDataSetChanged();
          }
      }
  
      public void performFavsSort() {
          FavoriteCurrencyListFragment frag = (FavoriteCurrencyListFragment) mSectionsPagerAdapter.getFragment(1);
          if (frag != null) {
              frag.performFavsSort();
          }
      }
  
      public void performAllCoinsSort() {
          AllCurrencyListFragment frag = (AllCurrencyListFragment) mSectionsPagerAdapter.getFragment(0);
          if (frag != null) {
              frag.performAllCoinsSort();
          }
      }
  
  }
  </code></pre>
  
  <p>This is the fragment which is throwing the error in "onAttach" method </p>
  
  <pre><code>public class AllCurrencyListFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener,
          SearchView.OnQueryTextListener {
  
      private SwipeRefreshLayout swipeRefreshLayout;
      private RecyclerView currencyRecyclerView;
      private AllCurrencyListAdapter adapter;
      private ArrayList&lt;CMCCoin&gt; currencyItemList;
      private ArrayList&lt;CMCCoin&gt; filteredList = new ArrayList&lt;&gt;();
      private MenuItem searchItem;
      private SearchView searchView;
      private View rootView;
      private Context mContext;
      public static String currQuery = "";
      ArrayList&lt;CMCCoin&gt; searchList;
      private HashMap&lt;String, String&gt; searchedSymbols = new HashMap&lt;&gt;();
      private HashMap&lt;String, Integer&gt; slugToIDMap = new HashMap&lt;&gt;();
      public static boolean searchViewFocused = false;
      private FavoritesListUpdater favsUpdateCallback;
      private SharedPreferences sharedPreferences;
  
      public interface FavoritesListUpdater {
          void removeFavorite(CMCCoin coin);
          void addFavorite(CMCCoin coin);
          void performFavsSort();
      }
  
      public AllCurrencyListFragment() {
      }
  
      public void performAllCoinsSort() {
          int sortType = sharedPreferences.getInt(SORT_SETTING, 1);
          sortList(adapter.getCurrencyList(), sortType);
          adapter.notifyDataSetChanged();
      }
  
      public void getQuickSearch() {
          CoinMarketCapService.getCMCQuickSearch(mContext, new afterTaskCompletion&lt;CMCQuickSearch[]&gt;() {
              @Override
              public void onTaskCompleted(CMCQuickSearch[] quickSearchNodeList) {
                  slugToIDMap = new HashMap&lt;&gt;();
                  Parcelable recyclerViewState;
                  recyclerViewState = currencyRecyclerView.getLayoutManager().onSaveInstanceState();
                  for (CMCQuickSearch node : quickSearchNodeList) {
                      slugToIDMap.put(node.getSlug(), node.getId());
                  }
                  if (searchViewFocused) {
                      for (CMCCoin coin: searchList) {
                          if (slugToIDMap.get(coin.getId()) != null) {
                              coin.setQuickSearchID(slugToIDMap.get(coin.getId()));
                          }
                      }
                      adapter.setCurrencyList(searchList);
                  } else {
                      for (CMCCoin coin : currencyItemList) {
                          if (coin.getId() != null &amp;&amp; slugToIDMap.get(coin.getId()) != null) {
                              coin.setQuickSearchID(slugToIDMap.get(coin.getId()));
                          }
                      }
                      adapter.setCurrencyList(currencyItemList);
                  }
                  int sortType = sharedPreferences.getInt(SORT_SETTING, 1);
                  sortList(adapter.getCurrencyList(), sortType);
                  adapter.notifyDataSetChanged();
                  favsUpdateCallback.performFavsSort();
                  currencyRecyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
                  swipeRefreshLayout.setRefreshing(false);
              }
          }, new afterTaskFailure() {
              @Override
              public void onTaskFailed(Object o, Exception e) {
                  Log.e("ERROR", "Server Error: " + e.getMessage());
                  swipeRefreshLayout.setRefreshing(false);
              }
          }, true);
      }
  
      @Override
      public void onRefresh() {
          getCurrencyList();
      }
  
      public static AllCurrencyListFragment newInstance() {
          return new AllCurrencyListFragment();
      }
  
      public void getCurrencyList() {
          swipeRefreshLayout.setRefreshing(true);
          CoinMarketCapService.getAllCoins(mContext, new afterTaskCompletion&lt;CMCCoin[]&gt;() {
              @Override
              public void onTaskCompleted(CMCCoin[] cmcCoinList) {
                  try {
                      if (searchViewFocused) { // Copy some code here to make the checks faster
                          searchedSymbols.clear();
                          searchList.clear();
                          for (CMCCoin coin : filteredList) {
                              searchedSymbols.put(coin.getSymbol(), coin.getSymbol());
                          }
                          for (CMCCoin coin : cmcCoinList) {
                              if (searchedSymbols.get(coin.getSymbol()) != null) {
                                  searchList.add(coin);
                              }
                          }
                      } else {
                          currencyItemList.clear();
                          currencyItemList.addAll(Arrays.asList(cmcCoinList));
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  getQuickSearch();
              }
          }, new afterTaskFailure() {
              @Override
              public void onTaskFailed(Object o, Exception e) {
                  Log.e("ERROR", "Server Error: " + e.getMessage());
                  swipeRefreshLayout.setRefreshing(false);
              }
          }, true);
      }
  
      @Override
      public void onResume() {
          super.onResume();
          getActivity().invalidateOptionsMenu();
      }
  
      @Override
      public void onAttach(Context context) {
          this.mContext = context;
          this.favsUpdateCallback = (FavoritesListUpdater) context;
          super.onAttach(context);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          rootView = inflater.inflate(R.layout.fragment_all_currency_list, container, false);
          setHasOptionsMenu(true);
          DatabaseHelperSingleton db = DatabaseHelperSingleton.getInstance(mContext);
          sharedPreferences = getContext().getSharedPreferences(SHAREDPREF_SETTINGS, MODE_PRIVATE);
          searchList = new ArrayList&lt;&gt;();
          // Setup currency list
          currencyRecyclerView = rootView.findViewById(R.id.currency_list_recycler_view);
          HorizontalDividerItemDecoration divider = new HorizontalDividerItemDecoration.Builder(mContext).build();
          currencyRecyclerView.addItemDecoration(divider);
          LinearLayoutManager llm = new LinearLayoutManager(mContext);
          llm.setOrientation(LinearLayoutManager.VERTICAL);
          currencyRecyclerView.setLayoutManager(llm);
          currencyItemList = new ArrayList&lt;&gt;();
          adapter = new AllCurrencyListAdapter(favsUpdateCallback, currencyItemList, db, (AppCompatActivity) mContext, new CustomItemClickListener() {
              @Override
              public void onItemClick(int position, View v) {
                  Intent intent = new Intent(mContext, CurrencyDetailsTabsActivity.class);
                  intent.putExtra(GraphFragment.ARG_SYMBOL, adapter.getCurrencyList().get(position).getSymbol());
                  intent.putExtra(GraphFragment.ARG_ID, adapter.getCurrencyList().get(position).getId());
                  intent.putExtra(GraphFragment.COIN_OBJECT, adapter.getCurrencyList().get(position));
                  mContext.startActivity(intent);
              }
          });
          currencyRecyclerView.setAdapter(adapter);
          // Setup swipe refresh layout
          swipeRefreshLayout = rootView.findViewById(R.id.currency_list_swipe_refresh);
          swipeRefreshLayout.setColorSchemeResources(R.color.myAccentColor);
          swipeRefreshLayout.setOnRefreshListener(this);
          swipeRefreshLayout.post(new Runnable() {
              @Override
              public void run() {
                  swipeRefreshLayout.setRefreshing(true);
                  getCurrencyList();
              }
          });
          return rootView;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch(item.getItemId()) {
              case R.id.sort_button:
                  int sortType = sharedPreferences.getInt(SORT_SETTING, 1);
                  new MaterialDialog.Builder(getActivity())
                          .title(R.string.sort_by)
                          .items(R.array.sort_options)
                          .dividerColorRes(R.color.myPrimaryColor)
                          .widgetColorRes(R.color.myPrimaryColor)
                          .buttonRippleColorRes(R.color.myPrimaryColor)
                          .itemsCallbackSingleChoice(sortType, new MaterialDialog.ListCallbackSingleChoice() {
                              @Override
                              public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                  sortList(adapter.getCurrencyList(), which);
                                  adapter.notifyDataSetChanged();
                                  SharedPreferences.Editor editor = sharedPreferences.edit();
                                  editor.putInt(SORT_SETTING, which);
                                  editor.apply();
                                  favsUpdateCallback.performFavsSort();
                                  Toast toast = Toast.makeText(getContext(), "Sorting by: " + text, Toast.LENGTH_SHORT);
                                  toast.show();
                                  return true;
                              }
                          })
                          .show();
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  
      @Override
      public boolean onQueryTextSubmit(String query) {
          return false;
      }
  
      @Override
      public boolean onQueryTextChange(String query) {
          currQuery = query;
          query = query.toLowerCase();
          filteredList.clear();
          for (CMCCoin coin : currencyItemList) {
              if (coin.getSymbol().toLowerCase().contains(query) || coin.getName().toLowerCase().contains(query)) {
                  filteredList.add(coin);
              }
          }
          adapter.setCurrencyList(filteredList);
          return true;
      }
  
      private void showInputMethod(View view) {
          InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
          if (imm != null) {
              imm.showSoftInput(view, 0);
          }
      }
  
      @Override
      public void onPrepareOptionsMenu(Menu menu) {
          if (searchView != null &amp;&amp; searchViewFocused) {
              ((AppCompatActivity)mContext).getSupportActionBar().setTitle("");
              searchView.requestFocusFromTouch();
              searchView.setIconified(false);
              searchView.setIconified(false);
              searchView.setQuery(currQuery, false);
              showInputMethod(rootView);
          }
      }
  
  
      @Override
      public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
          getActivity().getMenuInflater().inflate(R.menu.all_currency_list_tab_menu, menu);
          searchItem = menu.findItem(R.id.action_search);
          searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
          searchView.setOnQueryTextListener(this);
          // Detect SearchView icon clicks
          searchView.setOnSearchClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  searchViewFocused = true;
                  setItemsVisibility(menu, searchItem, false);
              }
          });
          // Detect SearchView close
          searchView.setOnCloseListener(new SearchView.OnCloseListener() {
              @Override
              public boolean onClose() {
                  searchViewFocused = false;
                  setItemsVisibility(menu, searchItem, true);
                  return false;
              }
          });
          if (searchViewFocused) ((AppCompatActivity)mContext).getSupportActionBar().setTitle("");
          super.onCreateOptionsMenu(menu, inflater);
      }
  
      private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
          for (int i = 0; i &lt; menu.size(); ++i) {
              MenuItem item = menu.getItem(i);
              if (item != exception) item.setVisible(visible);
          }
          if (!visible) {
              ((AppCompatActivity)mContext).getSupportActionBar().setTitle("");
          } else {
              ((AppCompatActivity)mContext).getSupportActionBar().setTitle(getResources().getString(R.string.app_name));
          }
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          searchViewFocused = false;
      }
  
      public AllCurrencyListAdapter getAdapter() {
          return this.adapter;
      }
  }
  </code></pre>
  
  <p>I've tried to look around for answers but could'nt find even one and this error is stressing me out since I can't fix it! App crashes when I try to start this fragment. Any help is appricated.</p>