无法实例化接收器java.lang.ClassNotFoundException

Gui*_*man 9 android broadcastreceiver bootcompleted

当我试图实例化我在启动时用来启动服务的接收器时,我的android应用程序出错了.错误很明显,找不到我的接收器的类文件.但是我的清单文件,包和所有内容的一切都没问题,我也不知道发生了什么.这是我的代码:

package dti.obd.reader;

import dti.obd.reader.service.MainService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver 
{
      @Override
      public void onReceive(Context context, Intent intent) 
      {
            Intent serviceIntent = new Intent(MainService.class.getName());
            context.startService(serviceIntent);
      }
}
Run Code Online (Sandbox Code Playgroud)

我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dti.obd.reader"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <service android:name=".service.MainService" >
            <intent-filter >
                <action android:name="dti.obd.reader.service.MainService" />
            </intent-filter>
        </service>

        <receiver android:name="dti.obd.reader.BootReceiver" >
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED" >
                </action>
            </intent-filter>
        </receiver>
    </application>

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

</manifest>
Run Code Online (Sandbox Code Playgroud)

有谁知道错误吗?似乎包和名字都可以......

小智 19

你必须把你Reciever的一些package.如果它在主包上,系统将无法实例化.

我有同样的问题.幸运的是,在互联网上搜索错误之前,我正在做另一个java项目.我刚刚意识到那里的bug与此类似.刚试过并且工作了.:)

  • 你能解释一下吗? (12认同)