我得到了以下问题:java.lang.InstantiationException以下内容

时间:2013-10-10 07:03:13

标签: android android-service

我正在通过闹钟呼叫服务。我得到 InstantiationException

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

2 个答案:

答案 0 :(得分:3)

您不应该定义Service类的参数构造函数。因此,请删除代码

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

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


虽然您可以使用空构造函数来进行服务。像

public DriverService() {
    super("DriverService");
}

答案 1 :(得分:0)

服务需要一个由Android

实例化的空构造函数
public DriverService() {
}

此外,无法将身份哈希传递给构造函数,因为您无法指定它。