I am developing an application in Android and have an issue where Android framework throws java.lang.InstantiationException upon trying to instantiate a nested class.
Here is the scenario.
-
Class A extending Activity with nested class B which extends BroadcastReceiver:
public class A extends Activity { public void onCreate(){ ....}; //Class B public class B extends BroadcastReceiver { public void onReceive(...) } } -
Class declarations in manifest file :
….
Sorry for the bad indentation. Just couldn’t find a way of properly indenting the code with so many tags.
- Broadcast from a service :
Intent updateIntent = new Intent();
updateIntent.setAction("desired_action");
sendBroadcast(accountPropertyUpdateIntent);
With all the above given things, the code gets compiled, but when run on the device, after the broadcast is called, i get a InstantiationException saying cannot instantiate pacakage.A$B, and dalvik says no found.
Now this whole scenario works on Android 2.2, but somehow this fails on 2.1.
I don’t know exactly what is happening. Am in need of help. Maybe something basic is missing by me.
Can anyone please help me? Thanks in advance.
Finally got a logical conclusion about the whole scenario myself. Was dumb enough not to read the
<receiver>documentation on developer.android.net.It clearly stated that there are two way for an application to get the broadcast.
I am using both the methods.
My scenario should have failed on 2.2 as well, as those receivers were registered through manifest too and they should have been called automatically, but somehow it didn’t ( This still remains a mystery ).
Removed all the receivers from manifest and just kept the dynamic registration of broadcast receivers and now the code works just like before only without the exceptions.
Thanks for all the help. 🙂