Error trying to save result to database

Asked

Viewed 71 times

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 returning, from the contact screen to the Activity in question, an error happens, I seem to not find the Intent, Activity, then returning to the Activity that was in the stack, before it. So, if you can help me check where the error is. Thank you very much, follow below 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 {projeto1.projeto_app/projeto1.projeto1_app.activity.ContatoActivity}: 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);
}

1 answer

0


The problem was within another method, which would alter the record. It was with a wrong logic so it would always fall into this method, which was blank, the code (ID).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.