tags:

views:

38

answers:

2

Hello all, I am having an inner class which extends BroadcastReceiver.

And I have added following line to AndroidManifest.xml:

<receiver android:name="OuterClass$InnerClass android:enabled="true"/>

But I am getting error Unable to instantiate receiver org.example.test.OuterClass$InnerClass:

Whats the issue?

A: 

Could it be that there is just a dot and a closing quote missing? Like

<receiver android:name=".OuterClass$InnerClass" android:enabled="true"/>
Martin
Nopes. Tried it. Doesnt work.
Kunal P.Bharati
A: 

The $ notation doesn't denote an inner class, but a static nested class. So there are in theory 2 ways to solve this particular problem:

  1. Denote it as a real inner class, i.e. OuterClass.InnerClass (not sure though if Android will eat that since the instantiation of an inner class is pretty more complex than just doing Class#newInstance().

  2. Declare the class to be a static nested class instead, i.e. add static to class InnerClass {}. This way the OuterClass$InnerClass must be able to create a new instance out of it.

If that doesn't solve the problem, then apparently Android simply doesn't eat it that way. I'd just extract it into its own standalone class then.

See also:

BalusC
Thanx for the answer BalusC :) But in my case I wanna call an Async class which is inner class of my activity using Alarm. Any ideas how to do it?
Kunal P.Bharati