Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ClassCastException:无法将MainActivity强制转换为侦听器_Java_Android_Android Fragments - Fatal编程技术网

Java ClassCastException:无法将MainActivity强制转换为侦听器

Java ClassCastException:无法将MainActivity强制转换为侦听器,java,android,android-fragments,Java,Android,Android Fragments,我正在尝试将WifiScanner侦听器实现到我的ScanFragment,但我收到以下错误:java.lang.ClassCastException:emilsoft.wifitest3.main活动无法强制转换到emilsoft.wifitest3.WifiScanner$Listener 我已经用正常的活动做过了,现在我正在尝试将它转换成片段,我正在学习这些片段。 我做了很多研究,但找不到有效的解决方案。我已经对出现错误的代码进行了注释 因此,我的主要活动是: private Sectio

我正在尝试将WifiScanner侦听器实现到我的ScanFragment,但我收到以下错误:
java.lang.ClassCastException:emilsoft.wifitest3.main活动无法强制转换到emilsoft.wifitest3.WifiScanner$Listener
我已经用正常的活动做过了,现在我正在尝试将它转换成片段,我正在学习这些片段。 我做了很多研究,但找不到有效的解决方案。我已经对出现错误的代码进行了注释

因此,我的主要活动是:

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

}
我的部分SpagerAdapter类

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;
}
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;
    }
我的扫描片段

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;
}
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;
    }
MyScanCollector类(处理添加到WifiScanner类的侦听器):

问题是我无法将正确的上下文传递给ScanCollector类,该类将随后将其强制转换为WifiScanner.Listener。可能是一个非常愚蠢的解决方案,但我找不到

提前谢谢

变化

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

this.getContext()
引用片段的当前上下文,它与真正实现
侦听器的主机活动的上下文不同


(确保
main活动实现WifiScanner.Listener

一个是
上下文,另一个是
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);

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


如果您将侦听器
附加到附件(…)
?他正在向
WifiScanner.listener
投射
上下文。虽然
Context
对象没有实现
WifiScanner.Listener
接口,但这并不能解决任何问题。为了让你的答案有效,他的活动应该实现听众interface@MD我认为他没有将侦听器连接到碎片上。我尝试了你的解决方案,应用程序构建成功,但我的wifi扫描结果没有显示@Pelocho医生的解决方案对我有效。无论如何,谢谢你的快速回答@MD你是对的,这是最好的方式。他很容易检测到某个活动是否没有实现其片段的侦听器之一。