0

这是参考这个链接

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

我的 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);
    }   
4

2 回答 2

0

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

于 2013-03-04T11:20:49.240 回答
0

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

您想按如下方式使用它:

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

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

于 2013-03-04T11:28:09.947 回答