简体   繁体   中英

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!

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

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 )

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

在此处输入图片说明

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM