ClassCastException:无法将MainActivity强制转换为Listener

时间:2016-03-05 15:17:12

标签: java android android-fragments

我正在尝试为我的ScanFragment实施WifiScanner侦听器,但我收到此错误:java.lang.ClassCastException: emilsoft.wifitest3.MainActivity cannot be cast to emilsoft.wifitest3.WifiScanner$Listener 我已经在正常的活动中做了这个,现在我试图将它转换为Fragments,我目前正在学习它们。 我做了很多研究,但我找不到合适的解决方案。我已经评论了存在错误的代码

所以主要活动

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

}

我的 SectionsPagerAdapter类

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

我的 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;
    }

我的 ScanCollector类(处理添加到WifiScanner类的侦听器):

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
}

问题是我无法将正确的Context传递给我的ScanCollector类,然后将其转换为WifiScanner.Listener。可能是一个非常愚蠢的解决方案,但我无法找到它。

提前致谢!

3 个答案:

答案 0 :(得分:6)

有一件事是context,另一件事是WifiScanner.Listener。你的ScanCollector需要两者都通过它们:

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

当你创建它时:

sc = new ScanCollector(getActivity(), this);

答案 1 :(得分:2)

更改

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

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

this.getContext()指的是片段的当前上下文,它与真正实现Listener接口的主机活动的上下文不同。

(确保MainActivity implements WifiScanner.Listener

答案 2 :(得分:0)

将实现添加到实现监听器的类中(您必须从示例中找到您的监听器名称)...然后单击Alt + Enter并从右键单击选项中选择实现方法

enter image description here

enter image description here