Unable to instantiate a nested class (extends broadcastReceiver) when a Broadcast…

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.

  1. 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(...)
  2. 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.

  1. Broadcast from a service :
Intent updateIntent = new Intent();

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

documentation on developer.android.net. It clearly stated that there are two way for an application to get the broadcast.

There are two ways to make a broadcast receiver known to the system:
One is declare it in the manifest file with this element.
The other is to create the receiver dynamically in code and register it with the
Context.registerReceiver() method.
See the BroadcastReceiver class description for more on dynamically created receivers.

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

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Unable to instantiate a nested class (extends broadcastReceiver) when a Broadcast…

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录