java.lang.InstantiationException:类没有无参数的构造 [英] java.lang.InstantiationException: class has no zero argument constructor
问题描述
我想使用一个BroadcastReceiver作为一个内部类来跟踪网络状态,但我得到了标题中的例外。我应该怎么做才能解决这个问题?
公共类NetworkChangeReceiver扩展广播接收器{
公共NetworkChangeReceiver(){
} @覆盖
公共无效的onReceive(上下文的背景下,意图意图){
最后ConnectivityManager connMgr =(ConnectivityManager)上下文
.getSystemService(Context.CONNECTIVITY_SERVICE); 最后android.net.NetworkInfo无线= connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 最后android.net.NetworkInfo移动= connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 如果(wifi.isAvailable()|| mobile.isAvailable()){
setupData();
Log.d(Netowk可用,旗1号);
} }
}
您
内广播接收机必须是静态的(通过清单登记)
块引用>或
非静态广播接收机必须注册和未注册的父类中。
块引用>这一点。
我用内广播reciver,未经类中注册它。要么让静态的,舱单注册,或使其不静,注册和注销的父类中。
I am trying to use a BroadcastReceiver as an inner class to track the network state but I got the exception in the title. What should I do to fix this problem?
public class NetworkChangeReceiver extends BroadcastReceiver { public NetworkChangeReceiver() { } @Override public void onReceive(Context context, Intent intent) { final ConnectivityManager connMgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (wifi.isAvailable() || mobile.isAvailable()) { setupData(); Log.d("Netowk Available ", "Flag No 1"); } } }
解决方案Your
inner Broadcast receiver must be static ( to be registered through Manifest)
OR
Non-static broadcast receiver must be registered and unregistered inside the Parent class
for this.
I was using an Inner broadcast reciver, without registering it within the class. Either make it static and register in Manifest , or Make it non static and register and unregister inside the parent class .
这篇关于java.lang.InstantiationException:类没有无参数的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!