【问题标题】:Android InstantiationException : No empty constructorAndroid InstantiationException:没有空的构造函数
【发布时间】:2014-01-16 17:21:07
【问题描述】:

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

【问题讨论】:

  • @Raghunandan:不,应该是正确的。
  • 您确定异常来自此版本的代码吗?您可能想完全卸载,然后再试一次,以确保安全。
  • @CommonsWare 你是正确的验证developer.android.com/reference/android/app/…
  • 你的意思是从模拟器中卸载应用程序?是的,我试过了。没有运气。
  • 嗯,从我坐的地方看,你所拥有的一切都很好。

标签: android intentservice instantiationexception android-intentservice


【解决方案1】:

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

【讨论】:

  • 这不是最终解决方案
猜你喜欢
  • 2016-11-12
  • 2013-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 1970-01-01
  • 2020-05-03
  • 1970-01-01
相关资源
最近更新 更多