user3496326
user3496326

Reputation: 131

Unable to instantiate receiver no empty constructor

I want to perform operation when the user has internet activated but i get the following error :

java.lang.InstantiationException: can't instantiate class com.project.proj.NetworkChangeReceiver; no empty constructor

In an activity class i have written the following

NetworkChangeReceiver net = new NetworkChangeReceiver(getBaseContext());

where am I going wrong. Please help !

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

    }
}
}

Upvotes: 0

Views: 2919

Answers (2)

nhaarman
nhaarman

Reputation: 100378

Remove:

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

Upvotes: 1

Boban S.
Boban S.

Reputation: 1662

BroadcastReceiver can't have other constructor except default. Delete constructor you created.

Also, you don't need to pass context to BroadcastReceiver, in onReceive parameter is context.

Upvotes: 5

Related Questions