2013-04-22 41 views
0

發送意圖到我的應用程序時出現「無法實例化接收器」錯誤。看來Eclipse似乎無法看到我的ExIntentReceiver類。我不知道爲什麼會發生。 ExIntentReceiver位於同一個包(com.example.app)中。無法實例化接收器

ExIntentReceiver.java:

package com.example.app 

public class ExIntentReceiver extends BroadcastReceiver { 

@Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
    if(action.equals("com.example.app.START_SERVICE")) { 
     Log.v("service", "is started"); 
     } else if(action.equals("com.example.app.STOP_SERVICE")) { 
      Log.v("service", "is stopped"); 
     } 

    } 
} 

清單:

<receiver android:name=".ExIntentReceiver" android:exported="true"> 
     <intent-filter> 
    <action android:name="com.example.app.START_SERVICE" /> 
     <action android:name="com.example.app.STOP_SERVICE" /> 
    </intent-filter> 
    </receiver> 

的logcat:

04-22 10:37:13.361: E/AndroidRuntime(11213): FATAL EXCEPTION: main 
04-22 10:37:13.361: E/AndroidRuntime(11213): java.lang.RuntimeException: Unable to instantiate receiver com.example.app.ExIntentReceiver: java.lang.ClassNotFoundException: com.example.app.ExIntentReceiver 
04-22 10:37:13.361: E/AndroidRuntime(11213): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2261) 
04-22 10:37:13.361: E/AndroidRuntime(11213): at android.app.ActivityThread.access$1600(ActivityThread.java:140) 
04-22 10:37:13.361: E/AndroidRuntime(11213): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313) 
04-22 10:37:13.361: E/AndroidRuntime(11213): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-22 10:37:13.361: E/AndroidRuntime(11213): at android.os.Looper.loop(Looper.java:137) 
04-22 10:37:13.361: E/AndroidRuntime(11213): at android.app.ActivityThread.main(ActivityThread.java:4921) 
04-22 10:37:13.361: E/AndroidRuntime(11213): at java.lang.reflect.Method.invokeNative(Native Method) 
04-22 10:37:13.361: E/AndroidRuntime(11213): at java.lang.reflect.Method.invoke(Method.java:511) 
04-22 10:37:13.361: E/AndroidRuntime(11213): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
04-22 10:37:13.361: E/AndroidRuntime(11213): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
04-22 10:37:13.361: E/AndroidRuntime(11213): at dalvik.system.NativeStart.main(Native Method) 
04-22 10:37:13.361: E/AndroidRuntime(11213): Caused by: java.lang.ClassNotFoundException: com.example.app.ExIntentReceiver 
04-22 10:37:13.361: E/AndroidRuntime(11213): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 
04-22 10:37:13.361: E/AndroidRuntime(11213): at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 
04-22 10:37:13.361: E/AndroidRuntime(11213): at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 
+3

類是'ExtIntentReceiver'but在你的清單文件'ExIntentReceiver'。 – rciovati 2013-04-22 09:12:07

回答

2

你應該在自己的意圖過濾器已經行動聲明。

<receiver android:name=".ExtIntentReceiver " android:exported="true"> 
    <intent-filter> 
     <action android:name="com.example.app.START_SERVICE" /> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="com.example.app.STOP_SERVICE" /> 
    </intent-filter> 
</receiver> 

除此之外確保你Receiver給出正確的包名內明顯。 並確保你應該拼寫正確。

0

如果你想有廣播接收器的內部類,你想用它未做它的實例你應該讓STATIC這樣

public static class MyBroadcastReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     //do what you want 
    } 
} 
相關問題