服务启动时崩溃

发布于 2025-01-04 17:05:42 字数 2146 浏览 0 评论 0原文

// 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);
  }
 }
}

这些都会在模拟器上返回错误,表示应用程序意外退出,我单击强制关闭,但主要活动没有关闭,所以我假设它是服务我正在强行关闭。下面是 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 库加载设置,并在主要活动失去焦点或关闭后继续将音频记录到文件中。

现在,如果我能推出一项简单的服务,我会很高兴。

// 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();
 }; 
};

tried to call the service using two methods:

method 1.

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

method 2

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

these both return an error on the emulator saying the app quit unexpectedly and i click force close, but the main activity doesn't close, so i'm assuming it's the service that i am force closing. below is the DDMS output:

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

just a couple simple questions.
what am i doing wrong?
what is the proper way to do this?

eventually i need the service to be capable of loading settings from a SQL base and continue recording audio to a file after the main activity loses focus or is closed.

for now i'd be happy if i could just launch a simple service.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2025-01-11 17:05:42

抽象类,无论是Service还是其他东西,都无法实例化。 Eclipse 无疑建议您将abstract 关键字添加到您的类中,因为您还没有实现创建具体实例所需的所有方法。

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

Abstract classes, be they Services or other things, cannot be instantiated. Eclipse no doubt suggested that you add the abstract keyword to your class because you had not implemented all the necessary methods to make a concrete instance.

I don't know if Eclipse has an option like this (I assume it does), but IntelliJ IDEA has an option where you can select "implement methods" when you have an incomplete class and it'll add all the stubs you need to fill in. Try that, or consult the documentation for a full list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文