Unable to instantiate receiver java.lang.ClassNotFoundException - android

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

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

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/

try:
<receiver android:name=".BootReceiver" >
It adds the package name itself because you defined:
package="dti.obd.reader"

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

Related

I renamed a package and my app won't launch

I've been following this tutorial to create an android game. I downloaded the skeleton he provided but changed one of my packages from robotgame to highst. Whenever I try and deploy the application with the example code to my device I get the following error.
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.ronyo.robotgame/com.ronyo.highst.Loader }
Error type 3
Error: Activity class {com.ronyo.robotgame/com.ronyo.highst.Loader} does not exist.
I've searched for an instance of robotgame in my project and I can't find an instance of it. The tutorial above is created using Eclipse and I'm porting it to Android Studio. I'm wondering if I've overlooked something when switching IDEs?
Here is my AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ronyo.highst"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:icon="#drawable/icon"
android:label="Loader" >
<activity
android:name=".Loader"
android:configChanges="keyboard|keyboardHidden|orientation"
android:label="Loader"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.inten.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Delete the folders .idea and .gradle then click button "Sync project with gradle files"
It looks like the package wasn't updated everywhere. Have you changed anything in the manifest?
In the AndroidManifest.xml file, in the <manifest tag, there should be a package attribute. That needs to be updated to match your new package name, com.ronyo.highst.
Check out the documentation here: http://developer.android.com/guide/topics/manifest/manifest-element.html
don't change it directly , It'll not update all the references, use this
right click on project > Android tools > Rename application package

WakefulBroadcastReceiver get java.lang.ClassNotFoundException

In my Android project, I have a Service class:
public class MyService extends Service{
...
//Defined a WakefulBroadcastReceiver as a inner static class
public static class DeviceRebootReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Do something when user reboot the device
}
}
}
As you see above, I have defined a WakefulBroadcastReceiver as a inner static class in MyService class. This receiver receives the broadcast when user reboot his/her device.
My AndroidManifest.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.project"
android:versionCode="1"
android:versionName="2.1" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
...>
<activity ...> ... </activity>
<service android:name="com.my.project.MyService"
android:exported="false"/>
<receiver
android:name="com.my.project.MyService$DeviceRebootReceiver"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
But when I run my app & after rebooted the device, my logcat shows me error:
Unable to instantiate receiver com.my.project.MyService$DeviceRebootReceiver:
java.lang.ClassNotFoundException: com.my.project.MyService$DeviceRebootReceiver in loader dalvik.system.PathClassLoader[/data/app/com.my.project-1.apk]
Why my receiver class is not able to be loaded by system?
=====Update======
I also tried to make my DeviceRebootReceiver as a separate class (DeviceRebootReceiver.java), and made the AndroidManifest.xml changed to :
<application...>
<receiver
android:name="com.my.project.DeviceRebootReceiver"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
But I still get the similar error in logcat:
java.lang.RuntimeException: Unable to instantiate receiver com.my.project.DeviceRebootReceiver: java.lang.ClassNotFoundException
I had the same problem and solved it.
It's about the android-support-v4.jar library, you should only have the official and updated library (copied from ANDROID_SDK/extras/android/support/) under the libs folder in your project and Android Private Libraries checked in the Java Build Path of your project. For example, I also had a ClassNotFoundException because of my project was using the android-support-v4.jar of another library (ActionBarSherlock) even with the official android-support-v4.jar included in my java build path, so I unchecked the Android Private Libraries of the ActionBarSherlock project to avoid this redundancy.

Can I add new package under the folder \scr in android?

I create an app with package name info.dodata.smsforward and eclipse create a default file construction just like Old.PNG.
I think the file construction under the folder \scr is not good, so I add three packages name BLL, DLL ,UI and place different classes file into different package name just like New.PNG.
My question are
A: Can I add new package under the folder \scr. You know there is only one package="info.dodata.smsforward" in AndroidManifest.xml, I don't konw if only one package name is allowed in one android app.
B: Are my packages name too short? Do I need add my domain before package name BLL, DAL and UI. Will the short package name (such as BLL, DLL) conflict with other APP?
Old ModePublic.java
package info.dodata.smsforward;
public class ModePublic {
public final static int DBVersion=2;
public final static String DBName="smsforward.db";
}
Old AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.dodata.smsforward"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="info.dodata.smsforward.UIMain"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
New ModePublic.java
package DAL;
public class ModePublic {
public final static int DBVersion=2;
public final static String DBName="smsforward.db";
}
New AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.dodata.smsforward"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="UI.UIMain"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
A. Yes you can, as many packages you like as deep as you like.
B. There are no rules on this. It's hard for other people to understand what you mean with such short names. So when working with more people it's probably better to have nicer names. They won't conflict with other apps because they only exists within your initial package name directory. (manifest)
Note: The AndroidManifest.xml package name serves as a unique identifier for the application. And create the initial folders where classes are generated in. For example bin/classes/info/dodata/smsforward/ Java packages are essentially just folders in src directory. You use those to bundle your interfaces and classes.
Yes i agree with #tim
A: You can add any number of sub packages inside a package - no issues
B:
You cannot add the main package name as BLL or DLL. The android need a main package name with at least 2 identifiers, so as to apply the application in google play store.
Only main package should be with 2 identifier, all other package names can be any type.
But good android coding approach should have a main package as com.xxx and other packages will be like com.xxx.view, com.xxx.activity, etc

Android/Sony Smartwatch: Unable to instantiate receiver (ClassNotFoundException)

I got an ClassNotFoundException with a static broadcastreceiver. The exception is fired when installing the app via eclipse on the device. I am developing an sony smartwatch extension, so I need to use the "SmartExtensionUtils" project from the sony sdk. As long as I was working inside the code example project everything was fine. I started a new project with Maven + Android and now it is no longer working!!! No idea why?!? I reckon something with the packages went wrong....
I got the following error:
E/AndroidRuntime(11201): java.lang.RuntimeException: Unable to instantiate receiver com.bla.move.smartwatch.sony.ExtensionReceiver: java.lang.ClassNotFoundException: com.bla.move.smartwatch.sony.ExtensionReceiver
My broadcastreceiver class is called ExtensionReceiver. The name inside the manifest file is the same ...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bla.move.smartwatch.sony"
android:versionName="1.0" android:versionCode="1">
<uses-sdk android:minSdkVersion="7"/>
<uses-permission
android:name="com.sonyericsson.extras.liveware.aef.EXTENSION_PERMISSION" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="SmartWatchPreferenceActivity" android:label="#string/preference_activity_title">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<service android:name=".SmartWatchExtensionService" />
<receiver android:name=".ExtensionReceiver">
<intent-filter>
.....
</intent-filter>
</receiver>
....
What could be the problem? Any suggestions??? I did alreday clean + rebuild (a couple of times)!
Did you include the SmartExtensionAPI and SmartExtensionUtil projects as dependencies, both in your project and in Maven?
Try cleaning the workspace (start Eclipse with the -clean flag), and make sure your project's JDK compliance settings is set to 1.6 (not 1.7)! That worked for me. I set the JDK compliance level of the whole workspace, instead of the project itself, and the error just disappeared!

how to catch the system broadcast BOOT_COMPLETED, my program just doesn't work?

I writed a small program to catch the system broadcast BOOT_COMPLETED, but it just doesn't work:
package com.alex.app.testsysreboot;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("my_tag", "system reboot completed.......");
}
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alex.app.testsysreboot"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
</manifest>
I closed the AVD, and then clicked the button "run" in Eclipse, and the Eclipse started a new AVD, but after the system boot, I just cannot see the log in the LogCat...
Well I tried this and it Works for me,
public class Autostart extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
Log.i("Autostart", "**********started************");
}
}
AndroidManifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pack.saltriver" android:versionCode="1" android:versionName="1.0"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>
You need to add
android:enabled="true"
android:exported="true"
AND
make sure that the app is not installed on the SD card - IIRC apps installed there don't receive that BOOT_COMPLETED.
Another point is that devices with "Fast Boot" enabled (like several HTC devices) (sometimes?) don't send BOOT_COMPLETED.
Since Android 3.1+ there is some more weirdness regarding BOOT_COMPLETED relating to "very first start of an app" - see http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
A working sample project with source see https://github.com/commonsguy/cw-advandroid/tree/master/SystemEvents/OnBoot
From http://arthurfmay.blogspot.com/2011/06/broadcastreceiver-bootcompleted-and.html
So instead, from Eclipse I just went into the Android SDK and AVD
Manager (under the Window Menu) and started the emulator from there. I
did this of course after loading the app into the emulator. I start
the emulator and my BroadcastReceiver on boot works just fine. There
was no need to go to running the emulator at the command line.
Another working sample can be found here.

Categories

Resources