gpt4 book ai didi

Android InstantiationException : No empty constructor

转载 作者:行者123 更新时间:2023-11-29 21:17:01 25 4
gpt4 key购买 nike

当我尝试启动 IntentService 时收到 InstantiationException。我在这里查看了其他线程,但答案并没有解决我的问题。我的服务类确实有一个默认构造函数。这是我的服务类。 (它在文件 FindMeService.java 中定义,这是该文件中唯一的类。)

package com.findme.findme;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;

public class FindMeService extends IntentService {

/*public static class Constants {

}*/

public static final String BROADCAST_ACTION = "com.findme.findme.BROADCAST";
public static final String ACTION_REGISTER_USER = "com.findme.findme.REGISTER_USER";
public static final String KEY_USERID = "user_id";
public static final String KEY_USERPASS = "user_pass";
public static final String RESPONSE_FOR = "response_for";
public static final String RESPONSE_VAL = "response_val";

public FindMeService() {
super("FindMeService");
}

@Override
protected void onHandleIntent(Intent intent) {
// TODO handle intent
if(intent.getAction() == ACTION_REGISTER_USER) {
CommunicationsManager commManager = new CommunicationsManager();

// get extras from the intent
Bundle details = intent.getExtras();
String userId = (String) details.get(KEY_USERID);
String password = (String) details.get(KEY_USERPASS);

// send the register request
String result = commManager.Register(userId, password);

// put the result into an intent and broadcast it
Intent resultIntent = new Intent(BROADCAST_ACTION);
resultIntent.putExtra(RESPONSE_FOR, ACTION_REGISTER_USER)
.putExtra(RESPONSE_VAL, result);
LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent);
}
}

我通过从 Activity 内部调用 startService() 来启动服务。日志内容如下:

E/AndroidRuntime( 1048): java.lang.RuntimeException: Unable to instantiate service 
com.findme.findme.FindMeService: java.lang.InstantiationException: can't instantiate
class com.findme.findme.FindMeService; no empty constructor

这是我启动服务的方式

...
Intent registerUserIntent = new Intent(this, FindMeService.class);
registerUserIntent
.setAction(FindMeService.ACTION_REGISTER_USER);
registerUserIntent.putExtra(FindMeService.KEY_USERID,
phoneNumber).putExtra(FindMeService.KEY_USERPASS,
password);

// start the service
startService(registerUserIntent);
...

这是 list 文件的相关部分

....
<service
android:name=".FindMeService"
android:exported="false"
android:enabled="true"/>
...

最佳答案

问题终于解决了,原来是模拟器或者Eclipse的bug导致的。在我重新启动 Eclipse 并取消选中“Launch form snapshot”选项并在启动模拟器之前选中“Wipe user data”选项后,问题就解决了。感谢大家关注这个问题。我也对整个事情感到非常困惑。

关于Android InstantiationException : No empty constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21168579/

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