1

当我尝试启动 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"/>
...
4

1 回答 1

2

问题终于解决了,事实证明这是由于模拟器或 Eclipse 中的一些错误造成的。在我重新启动 Eclipse 并取消选中“启动表单快照”选项并在启动模拟器之前选中“擦除用户数据”选项后,问题得到了解决。感谢大家调查这个问题。我也对整个事情感到完全困惑。

于 2014-01-17T12:59:18.220 回答