我想在用户启动互联网时执行操作,但我收到以下错误:
java.lang.InstantiationException:无法实例化类com.project.proj.NetworkChangeReceiver;没有空构造函数
在活动课程中,我写了以下内容
NetworkChangeReceiver net = new NetworkChangeReceiver(getBaseContext());
我在哪里出错了。请帮忙!
public class NetworkChangeReceiver extends BroadcastReceiver
{
Context mContext;
public NetworkChangeReceiver(Context context)
{
mContext= context;
}
@Override
public void onReceive(final Context context, final Intent intent) {
String status = NetworkUtil.getConnectivityStatusString(context);
if (status == "Wifi enabled")
{
Database details=new Database(mContext);
details.open();
List<String> detailsId;
detailsId=details.getRecords();
retrieveValuesFromListMethod1(detailsId);
Toast.makeText(context, "working properly", Toast.LENGTH_SHORT).show();
}
Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
}
public void retrieveValuesFromListMethod1(List detailsId)
{
Iterator itr = detailsId.iterator();
while(itr.hasNext())
{
//do something
}
}
}
答案 0 :(得分:5)
BroadcastReceiver
除了默认值之外不能有其他构造函数。删除您创建的构造函数。
另外,您不需要将上下文传递给BroadcastReceiver
,onReceive
参数是上下文。
答案 1 :(得分:1)
卸下:
Context mContext;
public NetworkChangeReceiver(Context context)
{
mContext= context;
}