【问题标题】:No empty constructor没有空的构造函数
【发布时间】:2026-02-24 10:35:01
【问题描述】:
我正在尝试使用远程服务来构建蓝牙聊天(如 Android 示例之一)。但是自从我上次修改后,应用程序没有启动,并且日志显示我的服务的错误 no empty constructor。我已阅读 this post 但我无法添加任何空构造函数。一定是我做错了什么,或者它对远程服务不起作用。
这是我的构造函数:
public RemoteService(Context context, Handler handler) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
}
谁能告诉我它有什么问题?
【问题讨论】:
标签:
java
android
android-service
【解决方案1】:
试试:
public RemoteService() {
super();
}
除非您的业务规则规定您不能真正拥有一个空的构造函数。然后你在某个地方打电话给new RemoteService(),而你不应该这样做。
【解决方案2】:
您必须提供默认构造函数,因为服务需要默认构造函数,然后您可以像以前一样使用参数化构造函数。
public class ReminderService extends IntentService {
----------------------------
public ReminderService() // Need to add the default constructor
{
super("ReminderService"); // Calling the super constructor
}
public RemoteService(Context context, Handler handler) //Then use your own constructor
{
mAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
}
----------------------------------
}
试试这个,如果您还有任何问题,请告诉我。
【解决方案3】:
感谢您的回答,但与此同时,有人帮我清理了代码,删除了构造函数并做了一些其他事情,但我并没有完全理解。
很抱歉,我无法准确解释出了什么问题,但我可以说是非参数构造函数产生了错误。