Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException: MainActivity cannot be cast to Listener

I'm trying to implement a WifiScanner Listener to my ScanFragment but I'm receveing this error: java.lang.ClassCastException: emilsoft.wifitest3.MainActivity cannot be cast to emilsoft.wifitest3.WifiScanner$Listener I already did this with normal Activities and now I'm trying to convert it to Fragments, which I'm currently learning about them. I did a lot of research but I couldn't find a working solution. I have commented the code where there is the error

So my Main Activity:

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

}

My SectionsPagerAdapter class :

public class SectionsPagerAdapter extends FragmentPagerAdapter{

public SectionsPagerAdapter(FragmentManager fm) {
    super(fm);
    }

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0: return ScanFragment.newInstance();
    }
    return null;
}

My ScanFragment :

public class ScanFragment extends Fragment implements WifiScanner.Listener {
   private ScanCollector sc;
   private WifiManager wifi;
   public ScanFragment() {}

    public static ScanFragment newInstance() {
        return new ScanFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View result = inflater.inflate(R.layout.fragment_scan_results, container, false);
        wifi = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
        sc = new ScanCollector(this.getContext()); //THE ERROR STARTS HERE
        return result;
    }

My ScanCollector class(which handle the listeners added to the WifiScanner class):

public class ScanCollector {

// The context wrapper that we'll use for accessing system services, receiving
// broadcasts from the WifiManager
private final Context context;

private WifiScanner.Listener listener;

public ScanCollector(Context context) {
    if (context == null)
        throw new NullPointerException();
    this.context = context;
    this.listener = (WifiScanner.Listener)context; //THE ERROR IS HERE
}

The problem is that I can't pass the correct Context to my ScanCollector class which will then cast it to a WifiScanner.Listener. Probably is a very stupid solution but I can't find it.

Thanks in advance!

like image 848
Automatik Avatar asked Dec 15 '22 08:12

Automatik


1 Answers

One thing is the context and another is the WifiScanner.Listener. Your ScanCollector needs both so pass both of them:

public ScanCollector(Context context, WifiScanner.Listener listener) {
    if (context == null)
        throw new NullPointerException();
    this.context = context;
    this.listener = listener
}

And when you create it:

sc = new ScanCollector(getActivity(), this);
like image 61
Alberto S. Avatar answered Apr 27 '23 18:04

Alberto S.