簡體   English   中英

ClassCastException:無法將 MainActivity 強制轉換為偵聽器

[英]ClassCastException: MainActivity cannot be cast to Listener

我正在嘗試為我的 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;
}

我的掃描片段

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
}

問題是我無法將正確的上下文傳遞給我的 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
}

當你創建它時:

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

改變

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

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

this.getContext()指的是fragment的當前上下文,不同於真正實現了Listener接口的宿主activity的上下文。

(確保MainActivity implements WifiScanner.Listener

將實現添加到您的類實現偵聽器(您必須從示例中找到您的偵聽器名稱)...然后單擊 Alt+Enter 並從右鍵單擊選項中選擇實現方法

在此處輸入圖片說明

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM