我在android 中监听wifi变化服务时,遇到错误 java.lang.IllegalAccessException,sth is not accessible from class android.app.ActivityThread,主要是app中其他地方调用了默认的构造函数,必须增加默认构造函数且访问权限为public
1.错误的部分代码
//通过一个BroadcastReceiver(android 四大组建之一),用于动态注册监听网络变化public class NetworkConnectChangedReceiver extends BroadcastReceiver{ OnWifiChangedListener changed; Activity act; public NetworkConnectChangedReceiver(Activity act){ this.act = act; } public void setChangedListener(OnWifiChangedListener changed) { this.changed = changed; } @Override public void onReceive(Context context, Intent intent) { if (null == intent.getAction()){ return ; } // TODO Auto-generated method stub if(intent.getAction().equals(WifiManager.rssI_CHANGED_ACTION)){ //signal strength changed } else if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){//wifi连接上与否 System.out.PRintln("网络状态改变"); NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); if (null == info.getState()){ return ; } if(info.getState().equals(NetworkInfo.State.DISCONNECTED)){ System.out.println("wifi网络连接断开"); if (act != null){ //CommonAction.showToastMessageL(act, "wifi网络连接断开"); } } else if(info.getState().equals(NetworkInfo.State.CONNECTED)){ WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if ((null != wifiInfo) && (null != wifiInfo.getSSID()) && (!wifiInfo.getSSID().contains("unknown"))){ //获取当前wifi名称 System.out.println("连接到网络 " + wifiInfo.getSSID()); if (act != null){// CommonAction.showToastMessageL(act, "连接到网络 " + wifiInfo.getSSID()); changed.onChange(wifiInfo.getSSID()); } } } } else if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)){//wifi打开与否 int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED); if(wifistate == WifiManager.WIFI_STATE_DISABLED){ System.out.println("系统关闭wifi"); //CommonAction.showToastMessageL(act, "系统关闭wifi"); changed.onClose(); } else if(wifistate == WifiManager.WIFI_STATE_ENABLED){ System.out.println("系统开启wifi"); //CommonAction.showToastMessageL(act, "系统开启wifi"); } } } public interface OnWifiChangedListener { void onChange(String wifiName); void onClose(); }//Activity 中使用上面BroadcastReceiver及注册的代码public class ComListActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_com_list); registerNetChangerReciver(); } private void registerNetChangerReciver() { IntentFilter filter = new IntentFilter(); filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); if (null == netChangeRec){ netChangeRec = new NetworkConnectChangedReceiver(this); netChangeRec.setChangedListener(new OnWifiChangedListener(){ @Override public void onChange(String wifiName) { // TODO 自动生成的方法存根 if (!connectWifiName.equals(wifiName)){// connected wifi is chanded CommonAction.showToastMessageL(ComListActivity.this, "连接到网络 " + wifiName); updateAutoMemorytData(); updateListViewData(); connectWifiName = wifiName; } } @Override public void onClose() { // TODO 自动生成的方法存根 connectWifiName = ""; } }); } registerReceiver(netChangeRec, filter); }}2.错误提示如下: FATAL EXCEPTION: main Process: com.chaoyuan.signalmachineconfig, PID: 4087 java.lang.RuntimeException: Unable to instantiate receiver com.chaoyuan.signalmachineconfig.NetworkConnectChangedReceiver: java.lang.IllegalAccessException: com.chaoyuan.signalmachineconfig.NetworkConnectChangedReceiver() is not accessible from class android.app.ActivityThread at android.app.ActivityThread.handleReceiver(ActivityThread.java:2565) at android.app.ActivityThread.access
3.错误修改说明 错误原因主要是app中其他地方调用了默认的构造函数,必须增加默认构造函数且访问权限为public:
public class NetworkConnectChangedReceiver extends BroadcastReceiver{
........//添加默认构造函数,且为publicpublic NetworkConnectChangedReceiver(){}public NetworkConnectChangedReceiver(Activity act){ this.act = act;}.......}
新闻热点
疑难解答