2

向我的应用程序发送意图时出现“无法实例化接收器”错误。似乎 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>

日志猫:

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)
4

2 回答 2

4

您应该在自己的意图过滤器中声明操作。

<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内部清单提供正确的包名称。并确保你应该正确拼写。

于 2013-04-22T09:15:58.530 回答
3

如果您想将 BroadcastReceiver 作为内部类并且您想在不创建实例的情况下使用它,您应该像这样使其成为 STATIC

public static class MyBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        //do what you want
    }
}
于 2017-10-05T18:45:45.510 回答