gpt4 book ai didi

java - 安卓广播接收器 : Unable to instantiate receiver - no empty constructor

转载 作者:太空宇宙 更新时间:2023-11-03 11:50:07 24 4
gpt4 key购买 nike

我的 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();
}
...

https://stackoverflow.com/a/10305338/1285325

关于java - 安卓广播接收器 : Unable to instantiate receiver - no empty constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21618041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com