2012-03-20 101 views
0

我正在使用android源码。我正在从其中一个应用程序发送广播,并在另一个应用程序的(Launcher)清单中为相同的应用程序写入了意图过滤器,如图所示。在Android中发送广播时发生异常

<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 

可有人请让我知道为什么发生这种情况,并为同一

+0

无法实例化类com.android.launcher2.LauncherModel;没有空的构造函数。请显示代码LauncherModel – 2012-03-20 12:29:39

+0

[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 2012-03-20 12:35:48

回答

1

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

<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()方法。

UPDATE

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

+0

谢谢。将检查并更新 – user264953 2012-03-20 17:52:39

+0

你的建议可以帮助我摆脱我在我的问题中发布的异常。空的施工单独帮助我摆脱了这个问题。但不幸的是,我的问题还没有解决。当我按照你的建议使用空的构造函数时,它会产生很多副作用,因为当我使用空的构造函数时,在这个类中会产生许多对象。我宁愿摆脱这个问题,而不使用空的构造函数,以便我有0副作用。 – user264953 2012-03-21 18:23:50

+0

不过,我接受您的解决方案,因为它可能会在未来帮助其他人。但是,请求某人在不使用空构造函数的情况下为其提供替代解决方案。 – user264953 2012-03-21 18:24:31