服务崩溃的开始

问题描述:

// PPS.java 

package com.domain.Servicecrasher; 
import android.app.Service; 
import android.view.Gravity; 
import android.widget.Toast; 
public abstract class PPS extends Service 
{ 
@Override 
public void onCreate() 
{ 
    Toast toasty = Toast.makeText(PPS.this, "service created!", 500); 
    toasty.setGravity(Gravity.CENTER, 0, 200); 
    toasty.show(); 
}; 
public void onDestroy() 
{ 
    Toast toasted = Toast.makeText(PPS.this, "service destroyed!", 500); 
    toasted.setGravity(Gravity.CENTER, 0, -200); 
    toasted.show(); 
}; 
}; 

尝试使用两种方法来调用服务:服务崩溃的开始

方法1.

{ 
{ 
    { 
    startService(new Intent(MainMenu.this, PPS.class)); 
    } 
} 
} 

方法2

{ 
{ 
    { 
    Intent startPPS = new Intent(MainMenu.this, PPS.class); 
    startPPS.putExtra("com.domain.Servicecrasher.PPS", false); 
    startService(startPPS); 
    } 
} 
} 

这些都在仿真器上返回一个错误说应用程序意外退出,我点击力量关闭,但主要活动没有关闭,所以我假设这是我强迫关闭的服务ING。下面是DDMS输出:

java.lang.RuntimeException: Unable to instantiate service com.domain.Servicecrasher.PPS 
at android.app.ActivityThread.handleCreateService(ActivityThread.java:1929) 
at android.app.ActivityThread.access$2500(ActivityThread.java:117) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:3683) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(Zygote.java:839) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
at dalvik.system.NativeStart.main(Native Method) 
caused by: java.lang.InstantiationException: com.domain.Servicecrasher.PPS 
at java.lang.Class.newInstaceImp1(Native Method) 
at java.lang.Class.newInstaceI(Class.java:1409) 
at android.app.ActivityThread.handleCreateService(ActivityThread.java:1926) 
... 10 more 

只是几个简单的问题。 我做错了什么? 这样做的正确方法是什么?

最终我需要该服务能够从SQL基础加载设置,并在主要活动失去焦点或关闭后继续将音频录制到文件。

现在我很高兴如果我可以发起一个简单的服务。

+0

完整的logcat statctrace。 – kosa 2012-02-09 21:51:56

+0

什么是“com.domain。** notlaunchingservice ** .PPS”?我们看不到这一点。另外,如果“com.domain。** Servicecrasher ** .PPS”是您尝试启动的服务,那么它是抽象的,无法创建。 – 2012-02-09 21:59:21

+0

发布了更多logcat。 notlaunchingservice是旧的项目名称......现在更改为Servicecrasher以反映当前的挂断。对困惑感到抱歉。如果我删除“抽象”我得到一个警告,它不会编译。我只包括“抽象”,因为日食暗示它。 – notgoodwithjava 2012-02-09 22:10:46

无法实例化抽象类,无论是Service还是其他事物。 Eclipse无疑建议您将abstract关键字添加到您的类中,因为您尚未实现所有必要的方法来创建具体实例。

我不知道Eclipse是否有这样的选项(我假设它),但IntelliJ IDEA有一个选项,您可以选择“实现方法”,当你有一个不完整的类,它会添加所有的需要填写的存根(stub)。试试看,或者查阅完整列表的文档。

+0

我忘了导入android.os.iBinder。现在工作!你真棒!我经过两天的尝试后第一次工作。非常感谢! – notgoodwithjava 2012-02-09 22:33:55

+0

@notgoodwithjava很高兴你的工作! – Argyle 2012-02-10 00:14:09