Unable to instantiate receiver java.lang.ClassNotFoundException
Asked Answered
D

4

10

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

Dissidence answered 7/3, 2012 at 12:17 Comment(0)
L
22

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. :)

Lila answered 23/8, 2012 at 12:29 Comment(1)
Can you explain it a little more?Beaverboard
M
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/

Manis answered 27/1, 2015 at 14:15 Comment(4)
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 errorVinni
We have got this exception in our application online. And it turns out these solutions can workaround.Ultimogeniture
what new name to your apk? seriously, you suggest us to publish a new app :DBoozer
@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 ;)Pass
B
1

try:

<receiver android:name=".BootReceiver" >

It adds the package name itself because you defined:

package="dti.obd.reader"
Broil answered 7/3, 2012 at 12:21 Comment(5)
I've already tried this way. Thats why i changed to especify again the package... but none of them work. Thanks for your help!Dissidence
You need to specify the package AND write receiver line this way. Did you both at the same time? Also have a look at coderanch.com/t/439875/Android/Mobile/… maybe you can spot something different. (This is unlikely to make any difference) but put user-permission line above the application line.Broil
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...Dissidence
This seems also wrong: <service android:name=".service.MainService" > <intent-filter > <action android:name="dti.obd.reader.service.MainService" /> </intent-filter> </service>Broil
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. ThanksDissidence
L
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

Laevo answered 13/4, 2017 at 12:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.