2012-03-20 28 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

相關問題