广播接收器作为内部类

时间:2013-03-04 11:02:38

标签: android inner-classes receiver

这是参考这个link

我在运行代码时遇到非法访问异常。我已经尝试了上述链接中给出的清单的所有可能性。

我的logcat错误如下:

   03-04 16:42:48.105: E/AndroidRuntime(808): FATAL EXCEPTION: main
03-04 16:42:48.105: E/AndroidRuntime(808): java.lang.RuntimeException: Unable to start receiver com.example.testapp.MainActivity$SmsReceiver: android.content.res.Resources$NotFoundException: String resource ID #0x0
03-04 16:42:48.105: E/AndroidRuntime(808):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
03-04 16:42:48.105: E/AndroidRuntime(808):  at android.app.ActivityThread.access$2400(ActivityThread.java:117)
03-04 16:42:48.105: E/AndroidRuntime(808):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
03-04 16:42:48.105: E/AndroidRuntime(808):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 16:42:48.105: E/AndroidRuntime(808):  at android.os.Looper.loop(Looper.java:123)
03-04 16:42:48.105: E/AndroidRuntime(808):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-04 16:42:48.105: E/AndroidRuntime(808):  at java.lang.reflect.Method.invokeNative(Native Method)
03-04 16:42:48.105: E/AndroidRuntime(808):  at java.lang.reflect.Method.invoke(Method.java:507)
03-04 16:42:48.105: E/AndroidRuntime(808):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-04 16:42:48.105: E/AndroidRuntime(808):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-04 16:42:48.105: E/AndroidRuntime(808):  at dalvik.system.NativeStart.main(Native Method)
03-04 16:42:48.105: E/AndroidRuntime(808): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
03-04 16:42:48.105: E/AndroidRuntime(808):  at android.content.res.Resources.getText(Resources.java:201)
03-04 16:42:48.105: E/AndroidRuntime(808):  at android.widget.Toast.makeText(Toast.java:258)
03-04 16:42:48.105: E/AndroidRuntime(808):  at com.example.testapp.MainActivity$SmsReceiver.onReceive(MainActivity.java:39)
03-04 16:42:48.105: E/AndroidRuntime(808):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
03-04 16:42:48.105: E/AndroidRuntime(808):  ... 10 more

我的代码:

 public class MainActivity extends Activity {   



      public static class SmsReceiver extends BroadcastReceiver{


        int i;
        String body ;
        String number ;
        String no = "15555215554";

                @Override
                public void onReceive(Context context, Intent intent) {
                    // TODO Auto-generated method stub
                    Bundle bundle = intent.getExtras();
                    SmsMessage[] msgs = null;
                    String str = "";
                    if (bundle != null)
                    {
                        // Retrieve the SMS.
                        Object[] pdus = (Object[]) bundle.get("pdus");
                        msgs = new SmsMessage[pdus.length];
                        for (i=0; i<msgs.length; i++)
                        {
                            Toast.makeText(context, +i, Toast.LENGTH_SHORT).show();
                            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);

                            if(msgs[i].getOriginatingAddress().equals(no))
                            {
                                body = msgs[i].getMessageBody();
                                number = msgs[i].getOriginatingAddress();
                            str += "SMS from " + msgs[i].getOriginatingAddress();
                            str += " :";
                            str += msgs[i].getMessageBody().toString();
                            str += "\n";
                            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();


                            }
                        }                     

                }

    }
    }





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }   

2 个答案:

答案 0 :(得分:0)

忽略这类问题。请不要将Receiver类用作内部类。使它成为单独的类并在Manifest中注册。并有适当的意图过滤器将为您的目标服务。

答案 1 :(得分:0)

根据您更新的问题,问题是您使用了错误的Toast.makeText()方法。

您想要使用this one,如下所示:

Toast.makeText(context, msgs[i], Toast.LENGTH_SHORT).show();

Nyou正在使用的makeText()版本需要一个字符串资源ID。