如何解决此错误(此类应提供默认构造函数)?

时间:2017-07-01 07:42:32

标签: java android wifi

错误:错误:此类应提供默认构造函数(不带参数的公共构造函数)

我是创建wifi直播接收器。它从签名的apk生成时间得到错误。我附上了我的代码。此类用于接收相同的路由器设备列表。并获得脱机连接。

 public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
        private WifiP2pManager manager;
        private WifiP2pManager.Channel channel;
        private MainActivity activity;
        String TAG = "WiFiDirectBroadcastReceiver";
        /**
    //     * @param manager WifiP2pManager system service
    //     * @param channel Wifi p2p channel
    //     * @param activity activity associated with the receiver
         */


        public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel,MainActivity activity) {
            super();
            this.manager = manager;
            this.channel = channel;
            this.activity = activity;
        }

         /*
         * (non-Javadoc)s
         * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
         * android.content.Intent)
         */

        @Override
        public void onReceive(Context context, Intent intent){
            String action = intent.getAction();
            Log.d(TAG, "P2P action - " + action);
            if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
                // UI update to indicate wifi p2p status.
                int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
                if(state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
                    // Wifi Direct mode is enabled
                    activity.setIsWifiP2pEnabled(true);
                }else{
                    activity.setIsWifiP2pEnabled(false);
                    activity.resetData();
                }
                Log.d(TAG, "P2P state changed - " + state);
            } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)){
                // request available peers from the wifi p2p manager. This is an
                // asynchronous call and the calling activity is notified with a
                // callback on PeerListListener.onPeersAvailable()
                if(manager != null) {
                    manager.requestPeers(channel, (WifiP2pManager.PeerListListener) activity.getFragmentManager().findFragmentById(R.id.frag_list));
                }
                Log.d(TAG, "P2P peers changed");

            } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){
                if(manager == null) {
                    return;
                }
                NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
                if(networkInfo.isConnected()) {
                    // we are connected with the other device, request connection
                    manager.requestConnectionInfo(channel,activity);

                }else{
                    // It's a disconnect
                    activity.resetData();
                }
            } else  if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)){
                DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager().findFragmentById(R.id.frag_list);
                if(fragment != null) {
                    fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

BroadcastReceiver没有默认构造函数,但是您的WiFiDirectBroadcastReceiver类在默认构造函数WiFiDirectBroadcastReceiver()中隐式调用它:

public WiFiDirectBroadcastReceiver() {
    // implicit super() call here, calls BroadcastReceiver(), which doesn't exist
}

您需要通过BroadcastReceiver调用super(...)现有构造函数中的一个,使用适当的参数,或者提供默认参数。

答案 1 :(得分:-3)

添加不带参数的公共构造函数。构造函数具有参数。添加另一个没有参数的构造函数只是一个空构造函数。

public WiFiDirectBroadcastReceiver(){

    }