0

this is with reference to this link

i am getting illegal access exception when i run the code. i have tried all the possibilities for the manifest as given in the above link.

my logcat error is as follows:

   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

my code:

 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);
    }   
Community
  • 1
  • 1
newbee
  • 409
  • 2
  • 12
  • 34
  • 2
    Is it public and static? – class stacker Mar 04 '13 at 11:04
  • move it to the parent / separate class, isn't a great idea to be inner class –  Mar 04 '13 at 11:05
  • it was private and static. i tried using public static but now its giving me this error `03-04 16:35:39.065: E/AndroidRuntime(773): java.lang.RuntimeException: Unable to start receiver com.example.testapp.MainActivity$SmsReceiver: android.content.res.Resources$NotFoundException: String resource ID #0x0` – newbee Mar 04 '13 at 11:07
  • @matheszabi: if i move it to the parent class, then it works when i receive an sms. but if i click on the app icon it gives a force close error – newbee Mar 04 '13 at 11:09
  • @newtoandroid But you're aware that the new error message you get has nothing to do with your receiver being defined by an inner class, but rather with a wring string ID? – class stacker Mar 04 '13 at 11:10
  • @class Stacker: thanks. but i am still not not able to figure out where the error is. could u please check my code n help if u can find it? – newbee Mar 04 '13 at 11:14
  • @newtoandroid I don't have your code. But the exception is telling that you try to access a string resource with a zero given as ID, which is invalid. You're certainly calling `Activity.getText()` somewhere and the parameter you pass is invalid. – class stacker Mar 04 '13 at 11:18
  • @Class Stacker: i have updated my question with the code and the new logcat – newbee Mar 04 '13 at 11:21

2 Answers2

0

To ignore this kind of problem. please do not use the Receiver class as inner class. make it separate class and register that in Manifest. and with proper intent filter will serve your goal.

Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47
  • what my app does is, when i receive a message, it starts another activity. this part works fine when i receive an sms if i have my receiver class as a separate class. but if i click on my app icon and start it, it gives me a force close error. so to avoid this, i was thinking if i could have the receiver class as a nested class and have a layout displayed inside the oncreate method of the parent class. – newbee Mar 04 '13 at 11:25
  • which activity you want to show when launching application make it launcher from manifest.and from broadcast receiver you can always start new activity. – Dinesh Prajapati Mar 04 '13 at 11:27
  • This discussion is besides the point. Once @newtoandroid changed his inner class declaration, the true bug in his implementation emerged. It doesn't make much difference if you have a reveiver as a separate class or as a public static inner class. Except for the question which class is loaded with what overhead. – class stacker Mar 04 '13 at 11:31
0

As per your updated question, the problem is that you use the wrong Toast.makeText() method.

You want to use this one as follows:

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

The version of makeText() Nyou were using expects a string resource ID.

class stacker
  • 5,357
  • 2
  • 32
  • 65
  • thanks. this was just an unwanted line. i had added it so that i come to know if there is a problem with the variable 'i'. i have removed it now, and its working fine. :) – newbee Mar 04 '13 at 11:33