无法实例化接收器没有空构造函数

时间:2014-04-24 12:41:09

标签: java android

我想在用户启动互联网时执行操作,但我收到以下错误:

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

    }
}
}

2 个答案:

答案 0 :(得分:5)

BroadcastReceiver除了默认值之外不能有其他构造函数。删除您创建的构造函数。

另外,您不需要将上下文传递给BroadcastReceiveronReceive参数是上下文。

答案 1 :(得分:1)

卸下:

Context mContext;
public NetworkChangeReceiver(Context context)
{
    mContext= context;
 }