服务无法转换为 android.app.Activity
service cannot be cast to android.app.Activity
我正在尝试访问服务中的 editText 和按钮。
我见过this answer and ,也尝试过合并和锻炼,但没有收获。我在这里错过了什么?我也看到了其他类似的答案,并且从过去 2 天开始就一直试图完成它,但都是徒劳的。任何其他实现它的方式也将受到欢迎。
相关代码如下:
MainActivity.java
btn_start.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view) {
startService(new Intent(getApplicationContext(), MyService.class));
}
});
MyService.java
Context context = MyService.this;
Activity activity = (Activity) context;
Button stopButton = activity.findViewById(R.id.btn_stop);
EditText editTextUsername = activity.findViewById(R.id.edit_name);
AndroidManifest.xml
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
</service>
但是我得到这个错误:
LOGCAT
11-14 02:55:07.835 5906-5906/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nwagh.myapplication, PID: 5906
java.lang.RuntimeException: Unable to start service
com.example.nwagh.myapplication.MyService@32189af with Intent {
cmp=com.example.nwagh.myapplication/.MyService }:
java.lang.ClassCastException:
com.example.nwagh.myapplication.MyService cannot be cast to
android.app.Activity
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3060)
at android.app.ActivityThread.access00(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
Caused by: java.lang.ClassCastException:
com.example.nwagh.myapplication.MyService cannot be cast to
android.app.Activity
at com.example.nwagh.myapplication.MyService.onStartCommand(MyService.java:68)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3043)
at android.app.ActivityThread.access00(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 11-14
02:55:07.844 5906-5906/? E/MQSEventManagerDelegate: failed to get
MQSService.
Here 是完整的代码,如果您需要更多参考。
服务不是活动。服务没有 UI 与之相关。
所以:
1) 您不能将服务投射到 activity。
2) 在您的服务中没有 UI 供您查找。
为什么服务需要按钮?服务 运行 在后台,没有用户交互。
我不知道你在做什么,但在 Android 中它是这样工作的:
1) activity 启动一项服务(就像您按下按钮一样)
2)服务在后台执行任务
3) 服务报告返回 activity 或终止
你找错人了 ;-) 你的 Service
不应该对你的 UI 有任何了解,也不应该想用它来捣乱。如果您想更改 Service
中的 UI,那么您需要将该任务委派给 Activity
。 Activity
负责UI。您可以使用几种有据可查的方法:
Activity
绑定到 Service
并且 Service
在 Activity
上执行回调
Service
发送一个广播 Intent
并且 Activity
注册一个监听器,在广播 Intent
时对它做出反应
- 使用事件总线实现
我正在尝试访问服务中的 editText 和按钮。
我见过this answer and
相关代码如下:
MainActivity.java
btn_start.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view) {
startService(new Intent(getApplicationContext(), MyService.class));
}
});
MyService.java
Context context = MyService.this;
Activity activity = (Activity) context;
Button stopButton = activity.findViewById(R.id.btn_stop);
EditText editTextUsername = activity.findViewById(R.id.edit_name);
AndroidManifest.xml
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
</service>
但是我得到这个错误: LOGCAT
11-14 02:55:07.835 5906-5906/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.nwagh.myapplication, PID: 5906 java.lang.RuntimeException: Unable to start service com.example.nwagh.myapplication.MyService@32189af with Intent { cmp=com.example.nwagh.myapplication/.MyService }: java.lang.ClassCastException: com.example.nwagh.myapplication.MyService cannot be cast to android.app.Activity at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3060) at android.app.ActivityThread.access00(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:5527) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) Caused by: java.lang.ClassCastException: com.example.nwagh.myapplication.MyService cannot be cast to android.app.Activity at com.example.nwagh.myapplication.MyService.onStartCommand(MyService.java:68) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3043) at android.app.ActivityThread.access00(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:5527) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 11-14 02:55:07.844 5906-5906/? E/MQSEventManagerDelegate: failed to get MQSService.
Here 是完整的代码,如果您需要更多参考。
服务不是活动。服务没有 UI 与之相关。
所以:
1) 您不能将服务投射到 activity。 2) 在您的服务中没有 UI 供您查找。
为什么服务需要按钮?服务 运行 在后台,没有用户交互。
我不知道你在做什么,但在 Android 中它是这样工作的:
1) activity 启动一项服务(就像您按下按钮一样) 2)服务在后台执行任务 3) 服务报告返回 activity 或终止
你找错人了 ;-) 你的 Service
不应该对你的 UI 有任何了解,也不应该想用它来捣乱。如果您想更改 Service
中的 UI,那么您需要将该任务委派给 Activity
。 Activity
负责UI。您可以使用几种有据可查的方法:
Activity
绑定到Service
并且Service
在Activity
上执行回调
Service
发送一个广播Intent
并且Activity
注册一个监听器,在广播Intent
时对它做出反应- 使用事件总线实现