Aut*_*tik 2 java android android-fragments
我正在尝试为我的ScanFragment实现一个WifiScanner侦听器,但我收到了这个错误:java.lang.ClassCastException: emilsoft.wifitest3.MainActivity cannot be cast to 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);
}
Run Code Online (Sandbox Code Playgroud)
我的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;
}
Run Code Online (Sandbox Code Playgroud)
我的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;
}
Run Code Online (Sandbox Code Playgroud)
我的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
}
Run Code Online (Sandbox Code Playgroud)
问题是我无法将正确的Context传递给我的ScanCollector类,然后将其转换为WifiScanner.Listener.可能是一个非常愚蠢的解决方案,但我找不到它.
提前致谢!
有一件事是context另一件事是WifiScanner.Listener.你的ScanCollector需求都通过了他们两个:
public ScanCollector(Context context, WifiScanner.Listener listener) {
if (context == null)
throw new NullPointerException();
this.context = context;
this.listener = listener
}
Run Code Online (Sandbox Code Playgroud)
当你创建它时:
sc = new ScanCollector(getActivity(), this);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5105 次 |
| 最近记录: |