Android BroadcastReceiver: unable to instantiate receiver - no empty constructor

I have a problem with BroadcastReceiver. If I declare an action in the manifest like this:

    <receiver android:name="com.app.activity.observer.DataEntryObserver" >
        <intent-filter>
            <action android:name= "@string/action_db_updated" />
        </intent-filter>
    </receiver>

where in strings.xml i have:

       <string name="action_db_updated">com.app.DB_UPDATED</string>

everything works well. But if I change it to:

    <receiver android:name="com.app.activity.observer.DataEntryObserver" >
        <intent-filter>
            <action android:name= "com.app.DB_UPDATED" />
        </intent-filter>
    </receiver>

I have this exception when the receiver is called:

java.lang.RuntimeException: cannot execute instance of receiver com.app.activity.observer.DataEntryObserver: java.lang.InstantiationException: cannot create instance of class com.app.activity.observer.DataEntryObserver; no empty constructor

I would keep the working version, but the Play Store did not allow me to publish the application, because it expects a string value, not a @ string / .. variable

my receiver is an outer class and is defined as:

   public class DataEntryObserver extends BroadcastReceiver{

private AppUsageLoader dLoader;


public DataEntryObserver(AppUsageLoader dLoader) {
    this.dLoader = dLoader;

    IntentFilter filter = new IntentFilter(
            ReaLifeApplication.ACTION_DB_UPDATED);
    dLoader.getContext().registerReceiver(this, filter);
}


@Override
public void onReceive(Context arg0, Intent arg1) {

    // Tell the loader about the change.
    dLoader.onContentChanged();

}

}

+3
3

, "" .

:

public static class DataEntryObserver extends BroadcastReceiver{
public DeviceAdminSampleReceiver() {
            super();
        }
...

fooobar.com/questions/139419/...

+9

, :

public class DataEntryObserver extends BroadcastReceiver{

    private AppUsageLoader dLoader;

    // Empty constructor
    public DataEntryObserver() { }

    public DataEntryObserver(AppUsageLoader dLoader) {
        this.dLoader = dLoader;

        IntentFilter filter = new IntentFilter(
                ReaLifeApplication.ACTION_DB_UPDATED);
        dLoader.getContext().registerReceiver(this, filter);
    }


    @Override
    public void onReceive(Context arg0, Intent arg1) {

        // Tell the loader about the change.
        dLoader.onContentChanged();

    }
}

, . , .

+5

need to clear the constructor

public DataEntryObserver() {
    this.dLoader = null;
}
-4
source

All Articles