【问题标题】:ClassNotFoundException when trying to start a Service from an Activity in Android尝试从 Android 中的 Activity 启动服务时出现 ClassNotFoundException
【发布时间】:2011-11-14 21:34:15
【问题描述】:

我正在尝试从我的活动中启动服务,如下所示:

startService(new Intent(MyActivity.this,
                MyService.class));

但是有一个问题。什么都没发生。我在幕后调试,我可以看到 BootClassLoader 在 ClassNotFoundException 上跳闸。我不知道这是怎么发生的,因为我的清单文件中的 Service 类是这样的:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true" android:enabled="true">
        <service android:name=".MyService" android:exported="false"></service>
        ...
    </application>

它与 MyActivity 在同一个包中。我在这里有点疯了。可能是什么问题?我调试到 MyService,但 onCreate()onStartCommand() 也没有被调用。

【问题讨论】:

    标签: android exception service


    【解决方案1】:

    这种废话有时会发生。尝试清理项目...

    另外,如果您从不同的包启动服务,请尝试使用完整的包,然后使用类名。

    Intent intent = new Intent();
    intent.setClassName("com.example.Something", "com.example.Something.MyService");
    startService(intent);
    

    【讨论】:

    • 我已经试过了。我什至重新启动了 Eclipse 和我的手机。我也在我的另一部手机上试过,但它一直抛出异常......
    【解决方案2】:

    只是一个想法:如果你去项目的“bin”文件夹,应该有你的“*.apk”文件。将其重命名为 zip、解压缩并调查是否有您的服务类。我怀疑 smth 在构建步骤出错了,你的课程不在 apk 中。删除 bin 文件夹中的所有内容并设法从头开始重建项目。然后再次解压/检查。

    【讨论】:

    • 谢谢它解决了我的问题....好吧,我也调用了 adb uninstall 来确保从我的机器人中清除错误代码。
    【解决方案3】:

    服务是否包含在某个包中?如果是这样,您可能需要在清单中使用 Service 类的完整路径。

    【讨论】:

      【解决方案4】:

      试试这个:

      startService(new Intent(this, MyService.class));
      

      如果还是有问题,在Manifest.XML文件中将包名添加到服务名的开头如下:

      <service android:name="mypackagename.MyService"></service>
      

      不知道为什么你有那个“导出”的代码。

      【讨论】:

        猜你喜欢
        • 2011-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-23
        • 2012-06-07
        相关资源
        最近更新 更多