Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to instantiate receiver java.lang.ClassNotFoundException

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

like image 269
Guilherme Gusman Avatar asked Mar 07 '12 12:03

Guilherme Gusman


3 Answers

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

like image 73
Yashar PourMohammad Avatar answered Sep 28 '22 01:09

Yashar PourMohammad


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/

like image 35
Gökçer Gökdal Avatar answered Sep 28 '22 00:09

Gökçer Gökdal


try:

<receiver android:name=".BootReceiver" >

It adds the package name itself because you defined:

package="dti.obd.reader"
like image 31
Caner Avatar answered Sep 28 '22 02:09

Caner