这是参考这个链接
运行代码时出现非法访问异常。我已经尝试了上面链接中给出的清单的所有可能性。
我的 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);
}