1

我想创建一个组以使当前设备平板电脑作为接入点,但是在回调方法中onGroupInfoAvailable()我得到了组的空指针。为什么?

manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = manager.initialize(this, getMainLooper(), null); 

manager.createGroup(channel, new ActionListener() {
    @Override
    public void onSuccess() {
        manager.requestGroupInfo(channel, new GroupInfoListener() {
             public void onGroupInfoAvailable(WifiP2pGroup group) {
            if(group != null)
               Toast.makeText(SasetBladderPadActivity.this, "Group Passphrase: " 
                                   + group.getPassphrase(), Toast.LENGTH_LONG).show();
            else
               Toast.makeText(SasetBladderPadActivity.this, "Group is null ", 
                                     Toast.LENGTH_LONG).show();
                }
            });
        }

        @Override
public void onFailure(int arg0) {
    // TODO Auto-generated method stub
            Toast.makeText(SasetBladderPadActivity.this, "Create group failed: " + arg0, \
                     Toast.LENGTH_LONG).show();
        }

    });
4

2 回答 2

0

我认为您需要实现 grouplistener 然后@override ongroupinfoavailable 方法。成功只是异步调用。实际回调需要修改。

于 2014-04-02T05:33:57.650 回答
0

是的,该组已创建但未显示。我会建议使用实现倒数计时器的技巧(或者您可以使用任何其他计时器)在一秒钟后启动 requestGroupInfo() 。修改后的代码将是:

 manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
 channel = manager.initialize(this, getMainLooper(), null); 

 manager.createGroup(channel, new ActionListener() {
    @Override
    public void onSuccess() {
            new CountDownTimer(1000, 1000) {
            public void onTick(long millisUntilFinished) {}
            public void onFinish() {
                manager.requestGroupInfo(channel, new GroupInfoListener() {
                    public void onGroupInfoAvailable(WifiP2pGroup group) {
                    if(group != null)
                    Toast.makeText(SasetBladderPadActivity.this, "Group Passphrase: "
                    + group.getPassphrase(), Toast.LENGTH_LONG).show();
                    else
                    Toast.makeText(SasetBladderPadActivity.this, "Group is null ",
                    Toast.LENGTH_LONG).show();
                    }
                });
            }
        }.start();
    }

    @Override
    public void onFailure(int arg0) {
    ...
于 2018-08-15T21:52:13.127 回答