系统尝试将我的 IntentService 转换为 Activity

The system tries to cast my IntentService to an Activity

我最近做了一些更改,虽然经过大量测试后我无法在我的任何设备上重现此崩溃,但它在用户中经常发生。它是不同的制造商,通常是 Android 6.0.1,但我见过一个 6.0.0 和 4.4.4。我不知道它发生在什么背景下。

Fatal Exception: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.xxx.xxxxx/com.xxx.xxxxx.importfile.HardPathService}: java.lang.ClassCastException: com.xxx.xxxxx.importfile.HardPathService cannot be cast to android.app.Activity
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3132)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5472)
       at android.app.ActivityThread.access00(ActivityThread.java:229)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1827)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:7325)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by java.lang.ClassCastException: com.xxx.xxxxx.importfile.HardPathService cannot be cast to android.app.Activity
       at android.app.Instrumentation.newActivity(Instrumentation.java:1096)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3122)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5472)
       at android.app.ActivityThread.access00(ActivityThread.java:229)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1827)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:7325)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

服务声明如下:

 <service
            android:name=".importfile.HardPathService"
            android:exported="true"
            android:process=":pathservice"/>

我现在意识到它不需要导出(它确实需要一个单独的进程),但这是崩溃时声明的方式。

我在它开始崩溃时所做的更改:

  1. 将目标和编译版本 SDK 从 23 更改为 25。
  2. 服务现在以从 onActivityResult 返回的重新调整用途的意图启动,该意图方便地已经设置了数据和剪辑数据。它过去常常以一个新的意图开始。它看起来像这样:

    requestedIntent.setClass(getActivity(), HardPathService.class); getActivity().startService(requestedIntent);

我想是因为再次使用了intent,但我不知道为什么它有时会失败,所以我宁愿在改变它之前先解释一下。

只需将数据和 clipData 复制到一个新的 Intent 中,崩溃就会完全消失。所以,不要重复使用您的意图。

Intent startIntent = new Intent(getActivity(), HardPathService.class);
startIntent.setData(requestedIntent.getData());
startIntent.setClipData(requestedIntent.getClipData());
getActivity().startService(startIntent);