我正在创建一个应用程序,其中两个设备将通过WiFip2p网络连接并交换文件。我正在
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.p2p.CONNECTION_STATE_CHANGE flg=0x24000010 (has extras) } in com.example.hp.letsshare.WiFiDirectBroadcastReceiver@23e260b
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:894)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to read from field 'android.net.wifi.p2p.WifiP2pManager$ConnectionInfoListener com.example.hp.letsshare.Client.connectionInfoListener' on a null object reference
at com.example.hp.letsshare.WiFiDirectBroadcastReceiver.onReceive(WiFiDirectBroadcastReceiver.java:47)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:884)
客户端代码是
WifiP2pManager.ConnectionInfoListener connectionInfoListener = new WifiP2pManager.ConnectionInfoListener() {
@Override
public void onConnectionInfoAvailable(WifiP2pInfo info) {
final InetAddress address = info.groupOwnerAddress;
if(!(info.groupFormed && info.isGroupOwner))
{
Toast.makeText(Client.this, "Client Started",Toast.LENGTH_SHORT).show();
ClientThread clientThread = new ClientThread(address.getHostAddress().toString(), portNumber);
clientThread.start();
}
}
};
onReceive在下面
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action))
{
//Checks if the wifi is turned on or off just to show a toast
//Not necessary for this app
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE,-1);
}else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action))
{
if(mWifiP2pManager==null)
{
return;
}
NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if(networkInfo.isConnected())
{
mWifiP2pManager.requestConnectionInfo(mChannel,mActivity.connectionInfoListener);
}
}else if(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action))
{
if(mActivity!=null)
{
mWifiP2pManager.requestPeers(mChannel,mActivity.peerlistListener);
}
if(sActivity!=null)
{
mWifiP2pManager.requestPeers(mChannel,sActivity.peerlistListener);
}
}else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action))
{
}
}
有人可以指出为什么我会收到此错误,如果可能的话,然后为此提供适当的解决方案?