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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21618041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:41:11  来源:igfitidea点击:

Android BroadcastReceiver: Unable to instantiate receiver - no empty constructor

javaandroidexceptionbroadcastreceiverinstantiationexception

提问by Luca S.

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

我有一个 BroadcastReceiver 的问题。如果我以这种方式在清单中声明操作:

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

where in the strings.xml I have:

在strings.xml中我有:

       <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 as the receiver is called:

我有这个例外,因为接收器被称为:

java.lang.RuntimeException: Unable to instantiate receiver com.app.activity.observer.DataEntryObserver: java.lang.InstantiationException: can't instantiate class com.app.activity.observer.DataEntryObserver; no empty constructor

java.lang.RuntimeException:无法实例化接收器 com.app.activity.observer.DataEntryObserver:java.lang.InstantiationException:无法实例化类 com.app.activity.observer.DataEntryObserver;没有空的构造函数

I would keep the working version but the Play store doesn't allow me to publish the app because it expects a string value and not a variable @string/..

我会保留工作版本,但 Play 商店不允许我发布应用程序,因为它需要一个字符串值而不是变量 @string/..

my receiver is an outerclass 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();

}

}

}

采纳答案by 130nk3r5

Make the class a static class, otherwise it's "seen" as part of the original containing class instance.

使类成为静态类,否则它会被“视为”原始包含类实例的一部分。

thus:

因此:

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

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

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

回答by ROXI

need to empty constructor

需要清空构造函数

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

回答by Roberto

You need an empty constructor like this:

你需要一个像这样的空构造函数:

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

    }
}

Although I'm not sure if keeping the non-empty constructor will generate the same error. If it does, you will have to remove it.

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