Unable to instantiate receiver in BroadcastReceive

2019-01-15 08:20发布

Why I have this error :

ERROR/AndroidRuntime(854): Uncaught handler: thread main exiting due to uncaught exception
ERROR/AndroidRuntime(854): java.lang.RuntimeException: Unable to instantiate receiver com.android.GPS21.SmsReceiver: java.lang.ClassNotFoundException: com.android.GPS21.SmsReceiver in loader dalvik.system.PathClassLoader@43d02ef0
ERROR/AndroidRuntime(854): Caused by: java.lang.ClassNotFoundException: com.android.GPS21.SmsReceiver in loader dalvik.system.PathClassLoader@43d02ef0

This is my onReceive events:

public void onReceive(Context context, Intent intent) {
  // TODO Auto-generated method stub
  Log.i(LOG_TAG, "Recieved a message");
  if (intent.getAction().equals(ACTION)) {
   // if(message starts with SMStretcher recognize BYTE)
   StringBuilder sb = new StringBuilder();

   // The SMS-Messages are 'hiding' within the extras of the Intent.
   Bundle bundle = intent.getExtras();
   if (bundle != null) {

    // Get all messages contained in the Intent
    // Telephony.Sms.Intents.getMessagesFromIntent(intent) does not
    // work anymore hence the below changes

    Object[] pduObj = (Object[]) bundle.get("pdus");
    SmsMessage[] messages = new SmsMessage[pduObj.length];
    for (int i = 0; i < pduObj.length; i++)
     messages[i] = SmsMessage.createFromPdu((byte[]) pduObj[i]);
    // Feed the StringBuilder with all Messages found.
    for (SmsMessage currentMessage : messages) {
     sb.append("SMS Received From: ");
     // Sender-Number
     sb.append(currentMessage.getDisplayOriginatingAddress());
     sb.append("\nMessage : ");
     // Actual Message-Content
     sb.append(currentMessage.getDisplayMessageBody());
    }
   }
   // Logger Debug-Output
   Log.i(LOG_TAG, "[SMSApp] onReceive: " + sb);

   // Show the Notification containing the Message.
   Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
  }

In debug that onReceive() is error.

I just make BroadcastReceiver to receive SMS and show in notification Toast..

And I try send SMS from DDMS and that error appear..

9条回答
聊天终结者
2楼-- · 2019-01-15 08:40

I had the same problem as well. In my case, my Virtual Device was corrupted.

Try creating a new one and running with it. Worked for me!

查看更多
疯言疯语
3楼-- · 2019-01-15 08:43

Your manifest claims you have a class named com.android.GPS21.SmsReceiver, and Android cannot find it.

查看更多
孤傲高冷的网名
4楼-- · 2019-01-15 08:44

Your broadcastReceiver class must be a public class by example

public class ReceptorLlamadas extends BroadcastReceiver

查看更多
戒情不戒烟
5楼-- · 2019-01-15 08:47

Make sure you have a public constructor without parameters. For example I have my own BroadcastReceiver implementation that receives some properties on the constructor. After I added a public default (without parameters) constructor, it works perfectly.

查看更多
时光不老,我们不散
6楼-- · 2019-01-15 08:52

you can write the receiver as "static". In the program my.sample, should write receiver " static SimpleSmsReceiver

查看更多
做自己的国王
7楼-- · 2019-01-15 08:56

I've got a same error. After I modified the "AndroidManifest.xml" like this, the error is resolved.

receiver android:name=".SimpleSmsReceiver"

->

receiver android:name=".SimpleSMSReceiver"

The error was like this.

...
01-13 10:23:10.787: ERROR/AndroidRuntime(378): java.lang.RuntimeException: Unable to instantiate receiver my.sample.SimpleSmsReceiver: java.lang.ClassNotFoundException: my.sample.SimpleSmsReceiver in loader dalvik.system.PathClassLoader[/data/app/my.sample-2.apk]
01-13 10:23:10.787: ERROR/AndroidRuntime(378):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2789)
01-13 10:23:10.787: ERROR/AndroidRuntime(378):     at android.app.ActivityThread.access$3200(ActivityThread.java:125)
01-13 10:23:10.787: ERROR/AndroidRuntime(378):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
查看更多
登录 后发表回答