当我尝试启动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);
}
}
}
我通过从活动内部调用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);
...
这是清单文件的相关部分
....
<service
android:name=".FindMeService"
android:exported="false"
android:enabled="true"/>
...
答案 0 :(得分:2)
问题终于解决了,结果是由于模拟器或Eclipse中的一些错误。在重新启动Eclipse并取消选中“启动表单快照”选项并在启动模拟器之前选中“擦除用户数据”选项后,问题得以解决。感谢每一个人都在研究这个问题。我对整个问题感到非常困惑。