0

当我尝试通过 adb 启动 IntentService 时,出现以下错误

E/AndroidRuntime( 2418): java.lang.RuntimeException: Unable to instantiate service 
com.myCompany.MyPackage.Service: java.lang.InstantiationException: 
com.myCompany.MyPackage.Service

当我启动正常的服务 ovre adb 时,一切正常。

无法通过 adb 启动意图服务的原因是什么?

public class NCService extends IntentService {

public NCService(String name) {
    super(name);
    // TODO Auto-generated constructor stub
}

NetworkStateReceiver nsr;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Toast.makeText(getBaseContext(), "onStartCommand", 0).show();
    nsr = new NetworkStateReceiver();

    return START_STICKY;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    Toast.makeText(getBaseContext(), "onCreate", 0).show();
    super.onCreate();
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    Toast.makeText(getBaseContext(), "onDestroy", 0).show();
    super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub

}

}

<uses-sdk android:minSdkVersion="10" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <service android:name=".NCService" />

    <receiver android:name=".NetworkStateReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>
</application>

问候西蒙

4

1 回答 1

2

您的服务需要一个默认的无参数构造函数,否则 Android 不知道作为名称参数传递的参数:

public NCService() {
    super("MyNCService");
}
于 2012-07-03T12:06:39.670 回答