0

I am working with android source. I am sending a broadcast from one of the applications and have written the intent filter for the same in another application's(Launcher) manifest as shown.

<receiver
       android:name="com.android.launcher2.LauncherModel"
       android:enabled="true"
       android:exported="true">
       <intent-filter>
           <action android:name="com.abc.THEMECHANGED" />
       </intent-filter>
   </receiver>

The class which extends receiver is com.android.launcher2.LauncherModel. I get the following exception in Launcher when the broadcast happens.

    01-01 00:01:45.101: WARN/dalvikvm(835): threadid=1: thread exiting with uncaught exception (group=0x40b131f8)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): FATAL EXCEPTION: main
01-01 00:01:45.109: ERROR/AndroidRuntime(835): java.lang.RuntimeException: Unable to instantiate receiver com.android.launcher2.LauncherModel:               java.lang.InstantiationException: can't instantiate class com.android.launcher2.LauncherModel; no empty constructor
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2108)
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at android.app.ActivityThread.access$1500(ActivityThread.java:125)
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at android.os.Looper.loop(Looper.java:137)
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at android.app.ActivityThread.main(ActivityThread.java:4368)
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at java.lang.reflect.Method.invokeNative(Native Method)
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at java.lang.reflect.Method.invoke(Method.java:511)
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at dalvik.system.NativeStart.main(Native Method)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): Caused by: java.lang.InstantiationException: can't instantiate class com.android.launcher2.LauncherModel; no empty  constructor
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at java.lang.Class.newInstanceImpl(Native Method)
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at java.lang.Class.newInstance(Class.java:1319)
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2103)
01-01 00:01:45.109: ERROR/AndroidRuntime(835):     ... 10 more

Can someone please let me know why this happens and the solution for the same

user264953
  • 1,837
  • 8
  • 37
  • 63
  • can't instantiate class com.android.launcher2.LauncherModel; no empty constructor. Please, show code of LauncherModel – Vyacheslav Shylkin Mar 20 '12 at 12:29
  • [Code here](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/2.2_r1.1/com/android/launcher2/LauncherModel.java#LauncherModel.onReceive%28android.content.Context%2Candroid.content.Intent%29) – user264953 Mar 20 '12 at 12:35

1 Answers1

1

For the name you should only need the path to the class from the base package structure. In the manifest under the manifest tag there should be a package attribute, this will be your base package. Example:

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jjnford.example"
    android:versionCode="1"
    android:versionName="@string/version" >

So if the receiver were in the package com.jjnford.example.lancher2 the manifest name should be:

<receiver
    android:name=".launcher2.LauncherModel"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.abc.THEMECHANGED" />
    </intent-filter>
</receiver>

Also since your receiver is instantiated by the system you need to have an empty constructor as the system will call your onRecieve() method when your specified intent is caught.

UPDATE

Here is an example of how to created the BroadcastReceiver that is created programatically (look for my answer as it contains the code).

Community
  • 1
  • 1
jjNford
  • 5,170
  • 7
  • 40
  • 64
  • your suggestion helps me get rid of the exception I posted in my question. the empty constructo alone helped me get rid of this issue. But unfortunately, my problem is not yet resolved. It creates numerous side effects when I use the empty constructor as you suggested, because numerous objects are effected in this class when I use an empty constructor. I prefer to get rid of this issue without using an empty constructor so that I have 0 side effects. – user264953 Mar 21 '12 at 18:23
  • Still, I am accepting your solution as it may help some others in future. But, request someone to suggest an alternate solution for the same without using an empty constructor. – user264953 Mar 21 '12 at 18:24
  • @user264953 You can still accomplish what you are trying to do. The above is a receiver that is instantiated by the System (ie you application is not running). You can programatically register a receiver and use a custom constructor... I think I have posted on this, I will look for the link and post it in an update to the solution for you :) – jjNford Mar 21 '12 at 18:47
  • @user264953 The reason you must have a blank constructor if instantiated by the system is because it would not know what parameter to feed you, doing it programatcially would fix this. – jjNford Mar 21 '12 at 18:50
  • with the code in the update you provided, do I need to still retain the receiver tag in manifest of receiver app(like the one I specified in my question)? – user264953 Mar 21 '12 at 19:28
  • @user264953 You won't need the manifest tag. The manifest has everything the system knows about in it (think Observer Patter). Since you are instantiating your receiver programatically you will not need it. Just make sure you set up your intent filter as done in the example code. – jjNford Mar 21 '12 at 20:12