安卓InstantiationException:没有空的构造 [英] Android InstantiationException : No empty constructor

查看:175
本文介绍了安卓InstantiationException:没有空的构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个InstantiationException当我尝试启动IntentService。我已经在这里看到其他线程但答案不解决我的问题。我的服务类也有一个默认的构造函数。这是我的服务类。 (它被定义在一个文件FindMeService.java,这就是该文件中的唯一一类。)

 包com.findme.findme;
进口android.app.IntentService;
进口android.content.Intent;
进口android.os.Bundle;
进口android.support.v4.content.LocalBroadcastManager;

公共类FindMeService扩展IntentService {

/ *公共静态类常量{

} * /

公共静态最后弦乐BROADCAST_ACTION =com.findme.findme.BROADCAST;
公共静态最后弦乐ACTION_REGISTER_USER =com.findme.findme.REGISTER_USER;
公共静态最后弦乐KEY_USERID =USER_ID;
公共静态最后弦乐KEY_USERPASS =user_pass;
公共静态最后弦乐RESPONSE_FOR =response_for;
公共静态最后弦乐RESPONSE_VAL =response_val;

公共FindMeService(){
    超级(FindMeService);
}

@覆盖
保护无效onHandleIntent(意向意图){
    // TODO手柄意图
    如果(intent.getAction()== ACTION_REGISTER_USER){
        CommunicationsManager commManager =新CommunicationsManager();

        //从意图演员
        捆绑细节= intent.getExtras();
        字符串userid =(字符串)details.get(KEY_USERID);
        字符串密码=(字符串)details.get(KEY_USERPASS);

        //发送注册请求
        字符串结果= commManager.Register(用户ID,密码);

        //结果放入意图和广播这
        意图resultIntent =新的意图(BROADCAST_ACTION);
        resultIntent.putExtra(RESPONSE_FOR,ACTION_REGISTER_USER)
            .putExtra(RESPONSE_VAL,结果);
        LocalBroadcastManager.getInstance(本).sendBroadcast(resultIntent);
    }
}
 

}

我从一个活动里面打电话给startService()启动该服务。 日志内容如下:

  E / AndroidRuntime(1048):java.lang.RuntimeException的:无法实例化服务
com.findme.findme.FindMeService:java.lang.InstantiationException:不能实例
类com.findme.findme.FindMeService;没有空的构造
 

下面是我如何启动服务

  ...
意图registerUserIntent =新的意图(这一点,FindMeService.class);
    registerUserIntent
            .setAction(FindMeService.ACTION_REGISTER_USER);
    registerUserIntent.putExtra(FindMeService.KEY_USERID,
            phoneNumber的).putExtra(FindMeService.KEY_USERPASS,
            密码);

    //启动服务
    startService(registerUserIntent);
...
 

下面的清单文件的相关部分。

  ....
<服务
        机器人:名称=。FindMeService
        机器人:出口=假
        机器人:启用=真/>
...
 

解决方案

问题终于解决,事实证明,这是因为无论是在模拟器或在Eclipse中的一些错误。问题得到解决后,我重新启动Eclipse和取消选中了启动形式快照选项,并启动仿真器之前检查的清除用户数据选项。感谢每一位为寻找到这个问题。我也完全被莫名其妙对整个事情。

I get an InstantiationException when I try to start an IntentService. I have looked at other threads here but the answers don't solve my problem. My service class does have a default constructor. Here's my service class. (It is defined in a file FindMeService.java and that's the only class in that file.)

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);
    }
}

}

I am starting the service by making a call to startService() from inside an activity. The logs read as follows:

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

Here's how I am starting the service

...
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);
...

Here's a relevant part of the manifest file

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

解决方案

The problem was finally solved and it turns out that it was due to some bug in either the emulator or in Eclipse. The problem was solved after I restarted Eclipse and unchecked the "Launch form snapshot" option and checked the "Wipe user data" option before launching the emulator. Thanks every one for looking into this question. I too was totally baffled about the entire matter.

这篇关于安卓InstantiationException:没有空的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆