.Resources$NotFoundException: String resource ID #0x3

Refresh

December 2018

Views

3.3k time

1

In my application when i pass an intent into another class,so that user can send sms messages and that data get stored in database,..and when user click on select contact button he get the contacts checked on which he send the messages,firstly it worked fine but when i added update code on button click it start giving back the error..and logcat is showing me the error

03-24 11:45:10.811: E/AndroidRuntime(18748): android.content.res.Resources$NotFoundException: String resource ID #0x3
        03-24 11:45:10.811: E/AndroidRuntime(18748):    at android.widget.Toast.makeText(Toast.java:311)

smsSend.java

    public class SmsSend extends Activity implements OnClickListener {
        BroadcastReceiver smsSentReciver, smsSentDelivery;
        static EditText edName, edMessage;
        static int ResultCode = 12;
        static ArrayList<String> sendlist = new ArrayList<String>();
        Button btnSaveForLater, btnStart, btnExternal, btnSelect;
        static TextView txt;
        static StringBuilder conct = new StringBuilder();
        static String contacts = "";
        String delim = ";";
        public static String Name;
        TextView ed;
        int i = 0;
        String rowid, rowid1, value;
        int j;
        String Name1, msg, msg1;
        String[] cellArray;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.create_camp);


            btnExternal.setOnClickListener(this);
            txt = (TextView) findViewById(R.id.textnum2);
            ActionBar actionBar = getActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);

            rowid = getIntent().getStringExtra("rowid");
            value = getIntent().getStringExtra("key");
            rowid1 = getIntent().getStringExtra("rowid1");
            entryData();

            edName.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    edName.setError(null);
                }
            });
            edMessage.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    edMessage.setError(null);
                }
            });
            txt.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    txt.setError(null);
                }
            });
            // Toast.makeText(getApplication(), contacts.toString(),
            // Toast.LENGTH_LONG).show();
            Name1 = edName.getText().toString();

            msg1 = edMessage.getText().toString();
        }

        @Override
        public void onBackPressed() {
            // TODO Auto-generated method stub
            super.onBackPressed();
            edName.setText(null);
            edMessage.setText(null);
            conct.delete(0, conct.length());
            contacts = null;
            txt.setText("0");
        }

        @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            unregisterReceiver(smsSentReciver);
            unregisterReceiver(smsSentDelivery);
        }

        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();

            smsSentReciver = new BroadcastReceiver() {

                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    // TODO Auto-generated method stub
                    switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "sms has been sent",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic Fail",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No Service",
                                Toast.LENGTH_SHORT).show();

                    default:
                        break;

                    }
                }

            };
            smsSentDelivery = new BroadcastReceiver() {

                @Override
                public void onReceive(Context context, Intent intent) {
                    // TODO Auto-generated method stub
                    switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "Sms Delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "Sms not Delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                    }
                }

            };
            registerReceiver(smsSentReciver, new IntentFilter("SMS_SENT"));
            registerReceiver(smsSentDelivery, new IntentFilter("SMS_DELIVERED"));

        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            DatabaseHelp entry = new DatabaseHelp(SmsSend.this);
            entry.open();
            switch (v.getId()) {

            case R.id.btnSelect:

                Intent a = new Intent(SmsSend.this, MainActivity.class);
                startActivityForResult(a,ResultCode);
                break;
            case R.id.btnExternal:
                Intent file = new Intent(SmsSend.this, File_Selecter.class);
                startActivity(file);
                break;



            case R.id.btnStart:

                Log.i("SMS", "Sendlist Size: " + sendlist.size());
                if (edName.getText().toString().length() == 0
                        || edName.getText().toString().length() == 0
                        || txt.getText().equals("0")) {
                    edName.setError("First name is required!");
                    edMessage.setError("Message is required!");
                    txt.setError("Contacts required!");
                } else {

                    /*  if (rowid1 != null
                                && edName.getText().toString().length() != 0
                                && edName.getText().toString().length() != 0
                                && txt.getText().equals("0")
                                && edName.getText().toString() == value) {
                            Toast.makeText(getApplication(), "update", Toast.LENGTH_LONG).show();
                            SmsManager smsManager = SmsManager.getDefault();

                            PendingIntent piSend = PendingIntent.getBroadcast(this,
                                    0, new Intent("SMS_SENT"), 0);
                            PendingIntent piDelivered = PendingIntent.getBroadcast(
                                    this, 0, new Intent("SMS_DELIVERED"), 0);

                            Log.i("SMS", "contacts: " + contacts);

                            contacts = conct.toString();

                            cellArray = contacts.split(";");

                            for (int a1 = 0; a1 < cellArray.length; a1++) {

                                smsManager.sendTextMessage(cellArray[i].toString(),
                                        null, msg1, piSend, piDelivered);

                            }
                            long ltt = Long.parseLong(rowid1);
                            entry.updateEntry(ltt, Name1, msg1, contacts);
                            Toast.makeText(getApplication(), "updated",
                                    Toast.LENGTH_LONG).show();

                        } else {*/
                            boolean diditwork1 = true;
                            try {
                                Toast.makeText(getApplication(), "insert", Toast.LENGTH_LONG).show();

                            SmsManager smsManager = SmsManager.getDefault();

                            PendingIntent piSend = PendingIntent.getBroadcast(this,
                                    0, new Intent("SMS_SENT"), 0);
                            PendingIntent piDelivered = PendingIntent.getBroadcast(
                                    this, 0, new Intent("SMS_DELIVERED"), 0);

                            Log.i("SMS", "contacts: " + contacts);

                            contacts = conct.toString();
                            //Toast.makeText(getApplication(), contacts.toString(), Toast.LENGTH_LONG).show();
                            cellArray = contacts.split(";");
                            Toast.makeText(getApplication(), cellArray.length, Toast.LENGTH_LONG).show();
                            for (int a1 = 0; a1 < cellArray.length; a1++) {

                                smsManager.sendTextMessage(cellArray[i].toString(),
                                        null, msg1, piSend, piDelivered);

                            }

                            DatabaseHelp entry1 = new DatabaseHelp(SmsSend.this);
                            entry1.open();

                            entry1.entryCreate(Name1, msg1, contacts);

                            entry1.close();
                        }
                     catch (Exception e) {
                        diditwork1 = false;
                        String erroe = e.toString();

                        Dialog d = new Dialog(this);
                        d.setTitle("Dang it!");
                        TextView tv = new TextView(this);
                        tv.setText(erroe);
                        d.setContentView(tv);
                        d.show();

                    } finally {
                        if (diditwork1) {
                            Dialog d = new Dialog(this);
                            d.setTitle("Heck Yeah!");
                            TextView tv = new TextView(this);
                            tv.setText("Success");
                            d.setContentView(tv);
                            d.show();
                        }
                    }

                }

                edName.setText("");
                edMessage.setText("");
                txt.setText("0");

                break;
            }
            }
        //}

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            if (requestCode == ResultCode) {

                if (resultCode == RESULT_OK) {
                        sendlist = data.getStringArrayListExtra("name");
                        Toast.makeText(getApplication(), sendlist.size(), Toast.LENGTH_LONG).show();
                    if (sendlist != null) {

                        for (int i = 0; i < sendlist.size(); i++) {
                            conct.append(sendlist.get(i).toString());
                            conct.append(delim);
                            //Toast.makeText(getApplication(), conct.toString(),Toast.LENGTH_LONG).show();
                        }

                    }

                }

                i = sendlist.size();
                txt.setText(Integer.toString(i));
                if (resultCode == RESULT_CANCELED) {

                }

            }
        }


    }

MainaActiivty.java

    public class MainActivity extends Activity implements OnItemClickListener {

        ArrayList<String> name1 = new ArrayList<String>();
        static ArrayList<String> phno1 = new ArrayList<String>();
        ArrayList<String> phno0 = new ArrayList<String>();
        MyAdapter ma;
        Button send;
        String[] cellArray = null;
        int[] str;
        int v = 0;
        String contacts;
        static int check1;
        ListView lv;
        int index;
        int top;
        StringBuilder b = new StringBuilder();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            ActionBar actionBar = getActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
            setContentView(R.layout.get);
        //  TextView txt = (TextView) findViewById(R.id.textView1);
            getAllCallLogs(this.getContentResolver());
            lv = (ListView) findViewById(R.id.lv);
            ma = new MyAdapter();
            lv.setAdapter(ma);
            lv.setOnItemClickListener(this);
            lv.setItemsCanFocus(false);
            lv.setTextFilterEnabled(true);
            // b = SmsSend.contacts;

            contacts = SmsSend.contacts;
            if (SmsSend.contacts != null) {
                cellArray = contacts.split(";");
                // Toast.makeText(getApplication(),contacts.toString(),
                // Toast.LENGTH_LONG).show();
                for (int i = 0; i < cellArray.length; i++) {

                    for (int j = 0; j < phno1.size(); j++) {

                        if (cellArray[i].equals(phno1.get(j))) {
                             Toast.makeText(getApplication(),cellArray[i].toString(),
                             Toast.LENGTH_LONG).show();
                            ma.setChecked(j, true);

                        }
                    }

                }
            }

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            // TODO Auto-generated method stub
            getMenuInflater().inflate(R.menu.contact_main, menu);

            return true;

        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // TODO Auto-generated method stub

            switch (item.getItemId()) {
            case R.id.addPage:

                break;

            }
            return super.onOptionsItemSelected(item);
        }

        @Override
        public void onBackPressed() {
            // TODO Auto-generated method stub

            StringBuilder checkedcontacts = new StringBuilder();
            System.out.println(".............." + ma.mCheckStates.size());
            for (int i = 0; i < name1.size(); i++)

            {
                if (ma.mCheckStates.get(i) == true) {
                    phno0.add(phno1.get(i).toString());
                    checkedcontacts.append(name1.get(i).toString());
                    checkedcontacts.append("\n");

                } else {
                    System.out.println("..Not Checked......"
                            + name1.get(i).toString());
                }

            }

            Intent returnIntent = new Intent();
            returnIntent.putStringArrayListExtra("name", phno0);
            setResult(RESULT_OK, returnIntent);
            Toast.makeText(getApplication(), phno0.size(), Toast.LENGTH_LONG).show();
            finish();

        }

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
            ma.toggle(arg2);

        }

        public void getAllCallLogs(ContentResolver cr) {

            Cursor phones = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                    null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " ASC");
            while (phones.moveToNext()) {
                String phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String name = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

                System.out.println(phoneNumber);

                name1.add(name);
                phno1.add(phoneNumber);

            }

            phones.close();

        }

        class MyAdapter extends BaseAdapter implements
                CompoundButton.OnCheckedChangeListener {
            public SparseBooleanArray mCheckStates;
            LayoutInflater mInflater;
            TextView tv1, tv;
            CheckBox cb;

            MyAdapter() {
                mCheckStates = new SparseBooleanArray(name1.size());
                mInflater = (LayoutInflater) MainActivity.this
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }

            // Save ListView state

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return name1.size();
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub

                return position;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub

                return 0;
            }

            @Override
            public View getView(final int position, View convertView,
                    ViewGroup parent) {
                // TODO Auto-generated method stub
                View vi = convertView;
                if (convertView == null)
                    vi = mInflater.inflate(R.layout.row, null);
                tv = (TextView) vi.findViewById(R.id.textView1);
                tv1 = (TextView) vi.findViewById(R.id.textView2);
                cb = (CheckBox) vi.findViewById(R.id.checkBox1);
                tv.setText(name1.get(position));
                tv1.setText(phno1.get(position));
                cb.setTag(position);

                cb.setChecked(mCheckStates.get(position, false));
                cb.setOnCheckedChangeListener(this);

                return vi;

            }

            public boolean isChecked(int position) {

                return mCheckStates.get(position, false);
            }

            public void setChecked(int position, boolean isChecked) {
                mCheckStates.put(position, isChecked);
            }

            public void toggle(int position) {
                setChecked(position, !isChecked(position));
            }

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                mCheckStates.put((Integer) buttonView.getTag(), isChecked);

            }
        }

    }

3 answers

0

The second param to Toast must be a String type.

Concatenate second param like this:

Toast.makeText(getApplication(), "" + sendlist.size(), Toast.LENGTH_LONG).show();
0
Toast.makeText(getApplication(), sendlist.size(), Toast.LENGTH_LONG).show();

sendlist.size() returns an int and passing an int to Toast.makeText() uses the overload that attempts to load a string resource with the given id.

Replace with e.g. Integer.toString(sendlist.size()) to toast the integer value.

You have the same issue in multiple places. Check all your Toast.makeText() calls.

4

try this one:

Toast.makeText(getApplication(), sendlist.size().tostring(), Toast.LENGTH_LONG).show();

or else:

Toast.makeText(getApplication(), sendlist.size().toString()+"", Toast.LENGTH_LONG).show();

bcoz it will allow second params in a string.. and also use getApplication instead of this one example :

Toast.makeText(getApplicationContext(),
                    "Error during request: " + e.getLocalizedMessage(),
                    Toast.LENGTH_LONG).show();

Thank you,,