Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android BroadcastReceiver:无法实例化接收器-没有空构造函数_Java_Android_Exception_Broadcastreceiver_Instantiationexception - Fatal编程技术网

Java Android BroadcastReceiver:无法实例化接收器-没有空构造函数

Java Android BroadcastReceiver:无法实例化接收器-没有空构造函数,java,android,exception,broadcastreceiver,instantiationexception,Java,Android,Exception,Broadcastreceiver,Instantiationexception,我的收音机有问题。如果我以这种方式在清单中声明操作: <receiver android:name="com.app.activity.observer.DataEntryObserver" > <intent-filter> <action android:name= "@string/action_db_updated" /> </intent-filter> </r

我的收音机有问题。如果我以这种方式在清单中声明操作:

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

}需要清空构造函数

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

您需要这样一个空构造函数:

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();

    }
}

虽然我不确定保留非空构造函数是否会产生相同的错误。如果是这样,您将不得不删除它。

将该类设置为静态类,否则它将“视”为原始包含类实例的一部分

因此:


这不是空构造函数。将BroadcastReceiver设置为静态类是一种糟糕的体系结构。它将阻止您调用其中的许多方法,包括
startActivity
如果接收者已经在清单中,为什么要注册接收者?
   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();

}
public DataEntryObserver() {
    this.dLoader = null;
}
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();

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