9

I got an error in my android application when it tries to instantiate a receiver that i use to start a service on boot up. The error is obvious, it can not find the class file of my receiver. But everything is ok with my manifest file, the packages and all and i have no clue what is happening. Here is my code:

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

And my manifest file:

<?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>

Does anyone knows the erro? It seems that the package and the names are all ok...

Guilherme Gusman
  • 556
  • 2
  • 7
  • 17

4 Answers4

21

You have to put your Receiver in some package. The system won't be able to instantiate it if it is on the main package.

I had the same problem. Fortunately before searching the error on internet I was doing another java project. I just realized that the bug in there was similar to this one. I tried it just now and worked. :)

Community
  • 1
  • 1
2

I have also faced with this problem. Adding full package name to receiver definition in manifest file didn't help. Problem was there was an old odex file corresponding to my apk file. Android system loads classes from odex file so can not find receiver class.

Workarounds:

  • Remove the old odex file, or
  • Give a new name to your apk

http://www.addictivetips.com/mobile/what-is-odex-and-deodex-in-android-complete-guide/

Gökçer Gökdal
  • 950
  • 1
  • 11
  • 18
  • 1
    I faced the same problem . In the previous version, I use a receiver name A, In current version, I have remove it, then get this error – Jonguo Mar 12 '15 at 07:19
  • 1
    We have got this exception in our application online. And it turns out these solutions can workaround. – Cobain Jul 04 '17 at 06:05
  • what new name to your apk? seriously, you suggest us to publish a new app :D – Emil May 29 '19 at 23:11
  • @batmaci I mean rename your artifact name (apk file name) no need to rename your package name. So you can publish same app. Google play only looks for app package names not the apk file names as you know ;) – Gökçer Gökdal May 31 '19 at 02:00
1

You have to put your Reciever in some package Instead Add the full path of the Reciever

 <receiver android:name="com.yourpackage.BootReceiver" >

It Sounds Weired but in my case it resolved the Issue

Hope Someone will be fruitful with this experience

Abhishek Chaubey
  • 2,960
  • 1
  • 17
  • 24
1

try:

<receiver android:name=".BootReceiver" >

It adds the package name itself because you defined:

package="dti.obd.reader"
Caner
  • 57,267
  • 35
  • 174
  • 180
  • I've already tried this way. Thats why i changed to especify again the package... but none of them work. Thanks for your help! – Guilherme Gusman Mar 07 '12 at 12:33
  • 1
    You need to specify the package `AND` write `receiver `line this way. Did you both at the same time? Also have a look at http://www.coderanch.com/t/439875/Android/Mobile/make-your-application-run-as maybe you can spot something different. (This is unlikely to make any difference) but put `user-permission` line above the `application` line. – Caner Mar 07 '12 at 12:41
  • I tried everything, but nothing works. I'm getting totally crazy about this error. I think its something with the name of the package, some restriction that i don't know... – Guilherme Gusman Mar 07 '12 at 13:24
  • 1
    This seems also wrong: – Caner Mar 07 '12 at 13:40
  • 1
    I got it. It had something to do with my virtual machine also. I created a new one and it worked. Also i made this changes u told me and i think this helped a lot, cause my manifest file was kinda messy. Thanks – Guilherme Gusman Mar 07 '12 at 14:09