在Android中发送广播时出现异常

时间:2012-03-20 12:17:12

标签: android android-layout android-intent android-widget

我正在使用android源代码。我正在从其中一个应用程序发送广播,并在另一个应用程序(Launcher)清单中编写了相同的intent过滤器,如图所示。

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

扩展接收器的类是com.android.launcher2.LauncherModel。广播发生时,我在Launcher中收到以下异常。

    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

有人可以让我知道为什么会发生这种情况以及相同的解决方案

1 个答案:

答案 0 :(得分:1)

对于名称,您只需要从基础包结构中获取类的路径。在manifest标签下的清单中应该有一个包属性,这将是你的基础包。例如:

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

因此,如果接收者在com.jjnford.example.lancher2包中,则清单名称应为:

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

此外,由于你的接收器是由系统实例化的,你需要有一个空的构造函数,因为当你捕获指定的意图时系统会调用你的onRecieve()方法。

<强>更新

Here is an example如何创建以编程方式创建的BroadcastReceiver(查找我的答案,因为它包含代码)。