我如何初始化WifiP2pmanager.channel?

时间:2018-05-27 15:42:09

标签: java android

package in.webxstudio.filesharing;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.p2p.WifiP2pManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;




public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

private WifiP2pManager manager;
private WifiP2pManager.Channel channel;
private BroadcastReceiver broadcastReceiver;
private IntentFilter mIntentFilter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    manager=(WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    Log.d(TAG, "onCreate: manager "+manager);
    manager.initialize(this,getMainLooper(),null);
    channel=manager.initialize(this,getMainLooper(),null);
    broadcastReceiver=new WiFiDirectBroadcastReceiver(manager,channel,this);


    mIntentFilter = new IntentFilter();
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

    manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Log.d(TAG, "onSuccess: Peered Discovered");
        }

        @Override
        public void onFailure(int reasonCode) {
            Log.d(TAG, "onFailure: Failure Discovering Peers Status Code "+reasonCode);

        }
    });


}

/* register the broadcast receiver with the intent values to be matched */
@Override
protected void onResume() {
    super.onResume();
    registerReceiver(broadcastReceiver, mIntentFilter);
}
/* unregister the broadcast receiver */
@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(broadcastReceiver);
}
}

我在第3行收到错误,如下所示..

05-27 20:59:23.807 9498-9498/in.webxstudio.filesharing E/AndroidRuntime: FATAL EXCEPTION: main
    Process: in.webxstudio.filesharing, PID: 9498
    java.lang.RuntimeException: Unable to start activity ComponentInfo{in.webxstudio.filesharing/in.webxstudio.filesharing.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.wifi.p2p.WifiP2pManager$Channel android.net.wifi.p2p.WifiP2pManager.initialize(android.content.Context, android.os.Looper, android.net.wifi.p2p.WifiP2pManager$ChannelListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.wifi.p2p.WifiP2pManager$Channel android.net.wifi.p2p.WifiP2pManager.initialize(android.content.Context, android.os.Looper, android.net.wifi.p2p.WifiP2pManager$ChannelListener)' on a null object reference
        at in.webxstudio.filesharing.MainActivity.onCreate(MainActivity.java:28)

我的代码有什么问题?

2 个答案:

答案 0 :(得分:1)

声明处理Wi-Fi所需的权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-feature
        android:name="android.hardware.wifi.direct"
        android:required="true" />

在你的片段活动中根据需要调整此代码:

private WifiP2pManager manager;
private boolean isWifiP2pEnabled = false;
private boolean retryChannel = false;

private final IntentFilter intentFilter = new IntentFilter();
private Channel channel;
private BroadcastReceiver receiver = null;

private void initWifiP2P() {
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

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

@Override
public void onResume() {
    super.onResume();
    receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
    registerReceiver(receiver, intentFilter);
}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(receiver);
}

答案 1 :(得分:1)

对于有相同问题的任何人。请确保您使用的是真正的android设备而不是模拟器来运行项目。