我的 BroadcastReceiver 有问题。如果我以这种方式在 list 中声明操作:
<receiver android:name="com.app.activity.observer.DataEntryObserver" >
<intent-filter>
<action android:name= "@string/action_db_updated" />
</intent-filter>
</receiver>
我在 strings.xml 中的位置:
<string name="action_db_updated">com.app.DB_UPDATED</string>
一切正常。但是如果我把它改成:
<receiver android:name="com.app.activity.observer.DataEntryObserver" >
<intent-filter>
<action android:name= "com.app.DB_UPDATED" />
</intent-filter>
</receiver>
我有这个异常(exception),因为接收器被调用:
java.lang.RuntimeException:无法实例化接收器 com.app.activity.observer.DataEntryObserver:java.lang.InstantiationException:无法实例化类 com.app.activity.observer.DataEntryObserver;没有空构造函数
我会保留工作版本,但 Play 商店不允许我发布应用程序,因为它需要一个字符串值而不是变量@string/..
我的接收者是一个外部类,定义为:
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 static class DataEntryObserver extends BroadcastReceiver{
public DeviceAdminSampleReceiver() {
super();
}
...
关于java - 安卓广播接收器 : Unable to instantiate receiver - no empty constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21618041/