Error trying to write result to database

1

I would like to record the Contact Name and contact number in the bank. So I can use it in other activities.

But when I return from the contact screen to the Activity in question, an error occurs, it seems to me that it does not find the Intent, the Activity, then return to the Activity that was in the stack, before it. So, if you can help me check where the error is. Thanks a lot, here are the codes and the error (There is only 1 given in the table):

  

java.lang.RuntimeException: Failure delivering result   ResultInfo {who = null, request = 1, result = -1, data = Intent {   dat = content: //com.android.contacts/contacts/lookup/0r1-4F314D4F31/1   flg = 0x1}} to activity   {project1.app_project / project1.project1_app.activity.ContactActivity}:   java.lang.NumberFormatException: Invalid int: "null"

   Button botaoBuscar = (Button) findViewById(R.id.btnBuscaContato1);
    botaoBuscar.setOnClickListener(new View.OnClickListener() {
        //buscar o Contato
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, NUMERO_BUSCA_CONTATO);
        }
    });

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

        String name;
        String num_tel;

        if(requestCode == NUMERO_BUSCA_CONTATO){
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor c = managedQuery(contactData, null, null, null, null);
                if (c.moveToFirst()) {
                    String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                    String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                    if (hasPhone.equalsIgnoreCase("1")) {
                        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[] { id },
                                null);
                        phones.moveToFirst();
                        int cNumber = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                        int cName = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);

                        name = phones.getString(cName);
                        num_tel = phones.getString(cNumber);

                        //System.out.println("number is:" + cNumber);
                        //TextView editTelefone = (TextView) findViewById(R.id.txtContato1);
                       // editTelefone.setText(cName);

                        if (CriaBanco.NOME_CONTATO.isEmpty() &&
                                CriaBanco.NUM_TEL_CONTATO.isEmpty()) {
                            insereRegistro(name,num_tel);
                        }
                        else {
                            alteraRegistro(name,num_tel);
                        }

            //textContato();

                    }
                }
            }
        }
    }
private void textContato() {

    criaBanco = new CriaBanco(getBaseContext());
    sqlNomeContato = "select nome_contato from tabela_contato";
    Cursor cursor = criaBanco.getReadableDatabase().rawQuery(sqlNomeContato, null);

    textView = (TextView) findViewById(R.id.txtContato1);
    if (cursor != null && cursor.moveToFirst()) {
            cursor.moveToFirst();
            textView.setText(cursor.getString(cursor.getColumnIndex("nome_contato")));

    }
    else {
        textView.setText("Contato 1");
    }

    cursor.close();
}

private void insereRegistro(String nome, String num_tel) {

    bancoController = new BancoController(ContatoActivity.this);
    bancoController.insereDadoContato(nome, num_tel);
}
    
asked by anonymous 23.06.2016 / 17:00

1 answer

0

The problem was inside another method, which would change the registry. I was having the wrong logic so I would always fall into this method, which was blank, the ID.

    
23.06.2016 / 19:06