0

运行此代码时,它会给我一个异常 RuntimeException。我还没有向 WakeWork.class 添加任何代码。关于为什么会爆炸的任何想法?

这是从我的“接收器上”警报中调用的。

   Intent i = new Intent(context, WakeWork.class);
   WakefulIntentService.sendWakefulWork(context, i);

清单.xml
<service android:name="com.PageP.WakeWork"></service>

WAKEWORK.CLASS

package com.PageP;

import android.content.Intent;

public class WakeWork extends WakefulIntentService {

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

@Override
public void onCreate(){
    super.onCreate();
}

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

}
}

错误

    Thread [<1> main] (Suspended (exception RuntimeException))  
        ActivityThread.handleCreateService(ActivityThread$CreateServiceData) line: 2346 
        ActivityThread.access$1600(ActivityThread, ActivityThread$CreateServiceData) line: 126  
        ActivityThread$H.handleMessage(Message) line: 1221  
        ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
        Looper.loop() line: 137 
        ActivityThread.main(String[]) line: 4560    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 511  
        ZygoteInit$MethodAndArgsCaller.run() line: 784  
        ZygoteInit.main(String[]) line: 551 
        NativeStart.main(String[]) line: not available [native method]  

长猫

04-22 10:32:04.127: W/AsyncTask(9004): com.PageP.GrabURL.doInBackground(GrabURL.java:36) 04-22 10:32:04.127: W/AsyncTask(9004): 在 com.PageP .GrabURL.doInBackground(GrabURL.java:1) 04-22 10:32:19.080: E/AndroidRuntime(9004): java.lang.RuntimeException: 无法实例化服务 com.PageP.WakeWork: java.lang.InstantiationException: 可以't 实例化类 com.PageP.WakeWork; 没有空构造函数 04-22 10:32:19.080: E/AndroidRuntime(9004): Caused by: java.lang.InstantiationException: can't instantiate class com.PageP.WakeWork; 没有空的构造函数

4

1 回答 1

0

改变:

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

至:

public WakeWork() {
    super("George");
}

(请随意用其他值代替名称)

正如堆栈跟踪显示的那样,Android 无法实例化您的服务,因为没有零参数的公共构造函数,这正是上述修复解决的问题。

于 2013-04-22T14:45:35.917 回答