java.lang.ClassCastException: MainActivity cannot be cast?










0















Here's the full log



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)


Here are my activities :



The fragment which starts and passes to the other fragment -



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






This is the fragment which is throwing the error in "onAttach" method



public class AllCurrencyListFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener,
SearchView.OnQueryTextListener

private SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView currencyRecyclerView;
private AllCurrencyListAdapter adapter;
private ArrayList<CMCCoin> currencyItemList;
private ArrayList<CMCCoin> filteredList = new ArrayList<>();
private MenuItem searchItem;
private SearchView searchView;
private View rootView;
private Context mContext;
public static String currQuery = "";
ArrayList<CMCCoin> searchList;
private HashMap<String, String> searchedSymbols = new HashMap<>();
private HashMap<String, Integer> slugToIDMap = new HashMap<>();
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<CMCQuickSearch>()
@Override
public void onTaskCompleted(CMCQuickSearch quickSearchNodeList)
slugToIDMap = new HashMap<>();
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 && 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<CMCCoin>()
@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<>();
// 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<>();
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)
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 && 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 < 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;




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.










share|improve this question


























    0















    Here's the full log



    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)


    Here are my activities :



    The fragment which starts and passes to the other fragment -



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






    This is the fragment which is throwing the error in "onAttach" method



    public class AllCurrencyListFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener,
    SearchView.OnQueryTextListener

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView currencyRecyclerView;
    private AllCurrencyListAdapter adapter;
    private ArrayList<CMCCoin> currencyItemList;
    private ArrayList<CMCCoin> filteredList = new ArrayList<>();
    private MenuItem searchItem;
    private SearchView searchView;
    private View rootView;
    private Context mContext;
    public static String currQuery = "";
    ArrayList<CMCCoin> searchList;
    private HashMap<String, String> searchedSymbols = new HashMap<>();
    private HashMap<String, Integer> slugToIDMap = new HashMap<>();
    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<CMCQuickSearch>()
    @Override
    public void onTaskCompleted(CMCQuickSearch quickSearchNodeList)
    slugToIDMap = new HashMap<>();
    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 && 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<CMCCoin>()
    @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<>();
    // 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<>();
    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)
    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 && 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 < 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;




    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.










    share|improve this question
























      0












      0








      0








      Here's the full log



      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)


      Here are my activities :



      The fragment which starts and passes to the other fragment -



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






      This is the fragment which is throwing the error in "onAttach" method



      public class AllCurrencyListFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener,
      SearchView.OnQueryTextListener

      private SwipeRefreshLayout swipeRefreshLayout;
      private RecyclerView currencyRecyclerView;
      private AllCurrencyListAdapter adapter;
      private ArrayList<CMCCoin> currencyItemList;
      private ArrayList<CMCCoin> filteredList = new ArrayList<>();
      private MenuItem searchItem;
      private SearchView searchView;
      private View rootView;
      private Context mContext;
      public static String currQuery = "";
      ArrayList<CMCCoin> searchList;
      private HashMap<String, String> searchedSymbols = new HashMap<>();
      private HashMap<String, Integer> slugToIDMap = new HashMap<>();
      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<CMCQuickSearch>()
      @Override
      public void onTaskCompleted(CMCQuickSearch quickSearchNodeList)
      slugToIDMap = new HashMap<>();
      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 && 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<CMCCoin>()
      @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<>();
      // 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<>();
      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)
      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 && 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 < 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;




      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.










      share|improve this question














      Here's the full log



      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)


      Here are my activities :



      The fragment which starts and passes to the other fragment -



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






      This is the fragment which is throwing the error in "onAttach" method



      public class AllCurrencyListFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener,
      SearchView.OnQueryTextListener

      private SwipeRefreshLayout swipeRefreshLayout;
      private RecyclerView currencyRecyclerView;
      private AllCurrencyListAdapter adapter;
      private ArrayList<CMCCoin> currencyItemList;
      private ArrayList<CMCCoin> filteredList = new ArrayList<>();
      private MenuItem searchItem;
      private SearchView searchView;
      private View rootView;
      private Context mContext;
      public static String currQuery = "";
      ArrayList<CMCCoin> searchList;
      private HashMap<String, String> searchedSymbols = new HashMap<>();
      private HashMap<String, Integer> slugToIDMap = new HashMap<>();
      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<CMCQuickSearch>()
      @Override
      public void onTaskCompleted(CMCQuickSearch quickSearchNodeList)
      slugToIDMap = new HashMap<>();
      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 && 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<CMCCoin>()
      @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<>();
      // 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<>();
      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)
      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 && 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 < 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;




      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.







      java android






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 23:02









      Steven RõgrêSteven Rõgrê

      256




      256






















          3 Answers
          3






          active

          oldest

          votes


















          0














          You need to have MainActivity ... implements AllCurrencyListFragment.FavoritesListUpdater



          The Fragment will attach to an Activity, so in other words, at com.steven.test.providers.priceticker.currencylist.AllCurrencyListFragment.onAttach(AllCurrencyListFragment.java:182), that Activity that you have used to load this Fragment, must implement that interface.



          Plus, a Fragment is not an extension of a Context object




          Here are my activities




          You showed two Fragments...






          share|improve this answer























          • Thank you so much! Your answer worked wonders.

            – Steven Rõgrê
            Nov 16 '18 at 0:07


















          0














          In public void onAttach(Context context), the context passed in seems to be of type com.steven.test.MainActivity which appears not to be compatible with com.steven.test.providers.priceticker.currencylist.AllCurrencyListFragment$FavoritesListUpdate when you try and cast it here:



           this.favsUpdateCallback = (FavoritesListUpdater) context;





          share|improve this answer























          • So what is the solution for it? It's nerve recking, seriously :/

            – Steven Rõgrê
            Nov 15 '18 at 23:24











          • I can't tell from what you provided. Showing the MainActivity class might help

            – Justin
            Nov 15 '18 at 23:48


















          0














          I don't know what are you trying to do.



          in your code, you try to cast



          FavoritesListUpdater 


          to Context



          and that what causes the error.
          I think you want to cast the context to MainActivity right?



           this.favsUpdateCallback = ((MainActivity) getActivity()).favsUpdateCallback ;


          Do you want to do it like that?






          share|improve this answer

























          • Yeah but adding your code snippet doesn't work.

            – Steven Rõgrê
            Nov 15 '18 at 23:25











          • you have to use Activity in casting instead of Context. use this this.favsUpdateCallback = ((MainActivity) getActivity()).favsUpdateCallback ;

            – Momen Zaqout
            Nov 16 '18 at 3:22











          • see the answer now, I edited it.

            – Momen Zaqout
            Nov 16 '18 at 3:23










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329095%2fjava-lang-classcastexception-mainactivity-cannot-be-cast%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You need to have MainActivity ... implements AllCurrencyListFragment.FavoritesListUpdater



          The Fragment will attach to an Activity, so in other words, at com.steven.test.providers.priceticker.currencylist.AllCurrencyListFragment.onAttach(AllCurrencyListFragment.java:182), that Activity that you have used to load this Fragment, must implement that interface.



          Plus, a Fragment is not an extension of a Context object




          Here are my activities




          You showed two Fragments...






          share|improve this answer























          • Thank you so much! Your answer worked wonders.

            – Steven Rõgrê
            Nov 16 '18 at 0:07















          0














          You need to have MainActivity ... implements AllCurrencyListFragment.FavoritesListUpdater



          The Fragment will attach to an Activity, so in other words, at com.steven.test.providers.priceticker.currencylist.AllCurrencyListFragment.onAttach(AllCurrencyListFragment.java:182), that Activity that you have used to load this Fragment, must implement that interface.



          Plus, a Fragment is not an extension of a Context object




          Here are my activities




          You showed two Fragments...






          share|improve this answer























          • Thank you so much! Your answer worked wonders.

            – Steven Rõgrê
            Nov 16 '18 at 0:07













          0












          0








          0







          You need to have MainActivity ... implements AllCurrencyListFragment.FavoritesListUpdater



          The Fragment will attach to an Activity, so in other words, at com.steven.test.providers.priceticker.currencylist.AllCurrencyListFragment.onAttach(AllCurrencyListFragment.java:182), that Activity that you have used to load this Fragment, must implement that interface.



          Plus, a Fragment is not an extension of a Context object




          Here are my activities




          You showed two Fragments...






          share|improve this answer













          You need to have MainActivity ... implements AllCurrencyListFragment.FavoritesListUpdater



          The Fragment will attach to an Activity, so in other words, at com.steven.test.providers.priceticker.currencylist.AllCurrencyListFragment.onAttach(AllCurrencyListFragment.java:182), that Activity that you have used to load this Fragment, must implement that interface.



          Plus, a Fragment is not an extension of a Context object




          Here are my activities




          You showed two Fragments...







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 23:40









          cricket_007cricket_007

          83.1k1145113




          83.1k1145113












          • Thank you so much! Your answer worked wonders.

            – Steven Rõgrê
            Nov 16 '18 at 0:07

















          • Thank you so much! Your answer worked wonders.

            – Steven Rõgrê
            Nov 16 '18 at 0:07
















          Thank you so much! Your answer worked wonders.

          – Steven Rõgrê
          Nov 16 '18 at 0:07





          Thank you so much! Your answer worked wonders.

          – Steven Rõgrê
          Nov 16 '18 at 0:07













          0














          In public void onAttach(Context context), the context passed in seems to be of type com.steven.test.MainActivity which appears not to be compatible with com.steven.test.providers.priceticker.currencylist.AllCurrencyListFragment$FavoritesListUpdate when you try and cast it here:



           this.favsUpdateCallback = (FavoritesListUpdater) context;





          share|improve this answer























          • So what is the solution for it? It's nerve recking, seriously :/

            – Steven Rõgrê
            Nov 15 '18 at 23:24











          • I can't tell from what you provided. Showing the MainActivity class might help

            – Justin
            Nov 15 '18 at 23:48















          0














          In public void onAttach(Context context), the context passed in seems to be of type com.steven.test.MainActivity which appears not to be compatible with com.steven.test.providers.priceticker.currencylist.AllCurrencyListFragment$FavoritesListUpdate when you try and cast it here:



           this.favsUpdateCallback = (FavoritesListUpdater) context;





          share|improve this answer























          • So what is the solution for it? It's nerve recking, seriously :/

            – Steven Rõgrê
            Nov 15 '18 at 23:24











          • I can't tell from what you provided. Showing the MainActivity class might help

            – Justin
            Nov 15 '18 at 23:48













          0












          0








          0







          In public void onAttach(Context context), the context passed in seems to be of type com.steven.test.MainActivity which appears not to be compatible with com.steven.test.providers.priceticker.currencylist.AllCurrencyListFragment$FavoritesListUpdate when you try and cast it here:



           this.favsUpdateCallback = (FavoritesListUpdater) context;





          share|improve this answer













          In public void onAttach(Context context), the context passed in seems to be of type com.steven.test.MainActivity which appears not to be compatible with com.steven.test.providers.priceticker.currencylist.AllCurrencyListFragment$FavoritesListUpdate when you try and cast it here:



           this.favsUpdateCallback = (FavoritesListUpdater) context;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 23:09









          JustinJustin

          1,0431512




          1,0431512












          • So what is the solution for it? It's nerve recking, seriously :/

            – Steven Rõgrê
            Nov 15 '18 at 23:24











          • I can't tell from what you provided. Showing the MainActivity class might help

            – Justin
            Nov 15 '18 at 23:48

















          • So what is the solution for it? It's nerve recking, seriously :/

            – Steven Rõgrê
            Nov 15 '18 at 23:24











          • I can't tell from what you provided. Showing the MainActivity class might help

            – Justin
            Nov 15 '18 at 23:48
















          So what is the solution for it? It's nerve recking, seriously :/

          – Steven Rõgrê
          Nov 15 '18 at 23:24





          So what is the solution for it? It's nerve recking, seriously :/

          – Steven Rõgrê
          Nov 15 '18 at 23:24













          I can't tell from what you provided. Showing the MainActivity class might help

          – Justin
          Nov 15 '18 at 23:48





          I can't tell from what you provided. Showing the MainActivity class might help

          – Justin
          Nov 15 '18 at 23:48











          0














          I don't know what are you trying to do.



          in your code, you try to cast



          FavoritesListUpdater 


          to Context



          and that what causes the error.
          I think you want to cast the context to MainActivity right?



           this.favsUpdateCallback = ((MainActivity) getActivity()).favsUpdateCallback ;


          Do you want to do it like that?






          share|improve this answer

























          • Yeah but adding your code snippet doesn't work.

            – Steven Rõgrê
            Nov 15 '18 at 23:25











          • you have to use Activity in casting instead of Context. use this this.favsUpdateCallback = ((MainActivity) getActivity()).favsUpdateCallback ;

            – Momen Zaqout
            Nov 16 '18 at 3:22











          • see the answer now, I edited it.

            – Momen Zaqout
            Nov 16 '18 at 3:23















          0














          I don't know what are you trying to do.



          in your code, you try to cast



          FavoritesListUpdater 


          to Context



          and that what causes the error.
          I think you want to cast the context to MainActivity right?



           this.favsUpdateCallback = ((MainActivity) getActivity()).favsUpdateCallback ;


          Do you want to do it like that?






          share|improve this answer

























          • Yeah but adding your code snippet doesn't work.

            – Steven Rõgrê
            Nov 15 '18 at 23:25











          • you have to use Activity in casting instead of Context. use this this.favsUpdateCallback = ((MainActivity) getActivity()).favsUpdateCallback ;

            – Momen Zaqout
            Nov 16 '18 at 3:22











          • see the answer now, I edited it.

            – Momen Zaqout
            Nov 16 '18 at 3:23













          0












          0








          0







          I don't know what are you trying to do.



          in your code, you try to cast



          FavoritesListUpdater 


          to Context



          and that what causes the error.
          I think you want to cast the context to MainActivity right?



           this.favsUpdateCallback = ((MainActivity) getActivity()).favsUpdateCallback ;


          Do you want to do it like that?






          share|improve this answer















          I don't know what are you trying to do.



          in your code, you try to cast



          FavoritesListUpdater 


          to Context



          and that what causes the error.
          I think you want to cast the context to MainActivity right?



           this.favsUpdateCallback = ((MainActivity) getActivity()).favsUpdateCallback ;


          Do you want to do it like that?







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 3:23

























          answered Nov 15 '18 at 23:10









          Momen ZaqoutMomen Zaqout

          5671410




          5671410












          • Yeah but adding your code snippet doesn't work.

            – Steven Rõgrê
            Nov 15 '18 at 23:25











          • you have to use Activity in casting instead of Context. use this this.favsUpdateCallback = ((MainActivity) getActivity()).favsUpdateCallback ;

            – Momen Zaqout
            Nov 16 '18 at 3:22











          • see the answer now, I edited it.

            – Momen Zaqout
            Nov 16 '18 at 3:23

















          • Yeah but adding your code snippet doesn't work.

            – Steven Rõgrê
            Nov 15 '18 at 23:25











          • you have to use Activity in casting instead of Context. use this this.favsUpdateCallback = ((MainActivity) getActivity()).favsUpdateCallback ;

            – Momen Zaqout
            Nov 16 '18 at 3:22











          • see the answer now, I edited it.

            – Momen Zaqout
            Nov 16 '18 at 3:23
















          Yeah but adding your code snippet doesn't work.

          – Steven Rõgrê
          Nov 15 '18 at 23:25





          Yeah but adding your code snippet doesn't work.

          – Steven Rõgrê
          Nov 15 '18 at 23:25













          you have to use Activity in casting instead of Context. use this this.favsUpdateCallback = ((MainActivity) getActivity()).favsUpdateCallback ;

          – Momen Zaqout
          Nov 16 '18 at 3:22





          you have to use Activity in casting instead of Context. use this this.favsUpdateCallback = ((MainActivity) getActivity()).favsUpdateCallback ;

          – Momen Zaqout
          Nov 16 '18 at 3:22













          see the answer now, I edited it.

          – Momen Zaqout
          Nov 16 '18 at 3:23





          see the answer now, I edited it.

          – Momen Zaqout
          Nov 16 '18 at 3:23

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329095%2fjava-lang-classcastexception-mainactivity-cannot-be-cast%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線