Automatik
Automatik

Reputation: 337

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!

Upvotes: 4

Views: 8212

Answers (3)

Dame Gjurchinoski
Dame Gjurchinoski

Reputation: 159

Add implements to your class implementing listener ( you must find your listener name from example ) ... Then click Alt+Enter and select implement methods from right click options

enter image description here

enter image description here

Upvotes: 2

Alberto S.
Alberto S.

Reputation: 7649

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

Upvotes: 8

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

Change

sc = new ScanCollector(this.getContext()); // The fragment's context

to

sc = new ScanCollector(getActivity()); // The activity's context

this.getContext() refers to the current context of the fragment, which is different than the context of the host activity that truly implements the Listener interface.

(Make sure that MainActivity implements WifiScanner.Listener)

Upvotes: 2

Related Questions