1

我试图在我的片段类中调用初始化方法,但它说 getMainLooper 没有为这个类定义。

private void initializeWiFiDirect() {
        wifiP2pManager = 
          (WifiP2pManager)getActivity().getSystemService(Context.WIFI_P2P_SERVICE);

        wifiDirectChannel = wifiP2pManager.initialize(this, getMainLooper(), 
                  new ChannelListener() {
                    public void onChannelDisconnected() {
                     initializeWiFiDirect();
                    }
                  }
                );
      }

请帮忙。

4

1 回答 1

2

你可以这样改——

this --> getActivity().getApplcationContext()  // basically context.
                                               // you could use BaseContext too. 

getMainLooper --> getActivity().getMainLooper()

Channel wifiDirectChannel = wifiP2pManager.initialize(getActivity().getApplicationContext(), getActivity().getMainLooper(), 
              new ChannelListener() {
                public void onChannelDisconnected() {
                 initializeWiFiDirect();
                }
              }
            );
于 2013-10-07T23:19:36.000 回答