1

我正在从警报中调用服务。我得到实例化异常

public class OnAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    // PullPendingRequests.acquireStaticLock(context);
    Toast.makeText(context, "Don't panik but your time is up!!!!.", Toast.LENGTH_LONG).show();
    Log.d("Taxeeta:PullPendingRequets", "CallService Location");
    context.startService(new Intent(context, DriverService.class));
}
}

我的服务代码

public class DriverService extends Service {
private String identityHash;

private WakeLock mWakeLock;

public DriverService(String identityHash) {
    this.identityHash = identityHash;
}
@Override
public IBinder onBind(Intent intent) {
    return null;
}

private void handleIntent(Intent intent) {
}

private class PollTask extends AsyncTask<Void, Void, Void> {
        //MY STUFF HERE
}
@Override
public void onStart(Intent intent, int startId) {
    handleIntent(intent);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    handleIntent(intent);
    return START_NOT_STICKY;
}
public void onDestroy() {
    super.onDestroy();
    mWakeLock.release();
}
}

编辑:我的清单代码

<service
        android:name=".DriverService"
        android:enabled="true"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Light.NoTitleBar" />

    <receiver
        android:name="com.taxeeta.support.OnAlarmReceiver"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.NOTIFY" />
        </intent-filter>
    </receiver>

编辑:下面的例外

10-10 12:56:09.550: E/AndroidRuntime(11886): FATAL EXCEPTION: main
10-10 12:56:09.550: E/AndroidRuntime(11886): java.lang.RuntimeException: Unable to instantiate service com.taxeeta.DriverService: java.lang.InstantiationException: com.taxeeta.DriverService
10-10 12:56:09.550: E/AndroidRuntime(11886):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1933)
10-10 12:56:09.550: E/AndroidRuntime(11886):    at android.app.ActivityThread.access$2500(ActivityThread.java:117)
10-10 12:56:09.550: E/AndroidRuntime(11886):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989)
10-10 12:56:09.550: E/AndroidRuntime(11886):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 12:56:09.550: E/AndroidRuntime(11886):    at android.os.Looper.loop(Looper.java:130)
10-10 12:56:09.550: E/AndroidRuntime(11886):    at android.app.ActivityThread.main(ActivityThread.java:3687)
10-10 12:56:09.550: E/AndroidRuntime(11886):    at java.lang.reflect.Method.invokeNative(Native Method)
10-10 12:56:09.550: E/AndroidRuntime(11886):    at java.lang.reflect.Method.invoke(Method.java:507)
10-10 12:56:09.550: E/AndroidRuntime(11886):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-10 12:56:09.550: E/AndroidRuntime(11886):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-10 12:56:09.550: E/AndroidRuntime(11886):    at dalvik.system.NativeStart.main(Native Method)
10-10 12:56:09.550: E/AndroidRuntime(11886): Caused by: java.lang.InstantiationException: com.taxeeta.DriverService
10-10 12:56:09.550: E/AndroidRuntime(11886):    at java.lang.Class.newInstanceImpl(Native Method)
10-10 12:56:09.550: E/AndroidRuntime(11886):    at java.lang.Class.newInstance(Class.java:1409)
10-10 12:56:09.550: E/AndroidRuntime(11886):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1930)
10-10 12:56:09.550: E/AndroidRuntime(11886):    ... 10 more
4

2 回答 2

3

您不应定义 Service 类的参数构造函数。所以删除代码

public DriverService(String identityHash) {
    this.identityHash = identityHash;
}

因为你添加了构造函数,所以android找不到空的构造函数。所以删除它


尽管您可以对服务使用空构造函数。像

public DriverService() {
    super("DriverService");
}
于 2013-10-10T07:32:59.323 回答
0

服务需要一个空的构造函数才能被 Android 实例化

public DriverService() {
}

此外,无法将 identityHash 传递给构造函数,因为您在任何地方都没有指定它。

于 2013-10-10T07:39:34.533 回答