Pregunta

Please I get a NullPoiterException when I try to get the extras form the intent. The error is thrown exactly when I call

int posizione2 =Integer.parseInt((getIntent().getExtras().getString(Intent.EXTRA_TEXT)));

Any help very much appreciated!

private void aggiungiImm(View arg1, int arg2) {
    Intent i=newintent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
    i.putExtra(Intent.EXTRA_TEXT, Integer.toString(arg2));
    startActivityForResult(i, PICK_REQUEST);    
}    

@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
    Uri contactData=null;
    if (requestCode==PICK_REQUEST) {
    if (resultCode==RESULT_OK) {
    contactData=data.getData();                     
    int posizione2 =Integer.parseInt((getIntent().getExtras().getString(Intent.EXTRA_TEXT)));

EDITED: No solution so far. Can it be that the problem is related to the fact that this is an Implicit Intent??

¿Fue útil?

Solución 2

instead of

int posizione2 =Integer.parseInt((getIntent().getExtras().getString(Intent.EXTRA_TEXT)));

Try that:

Bundle extras = getIntent().getExtras();
if (extras==null) {
  Log.e( "", "No extras provided" );
  return;
}

String myText = extras.getString(Intent.EXTRA_TEXT);
if (myText==null) {
  Log.e( "", "No text provided" );
  return;
}

int posizione2 = Integer.parseInt(myText);

Should help you to see what is giving the exception...

By the way, your returned text (if that text is comming as result from the other activity) will be available in the Intent passed to the function. So you should be doing:

if (data.getString(Intent.EXTRA_TEXT)==null) {
  Log.e( "", "No text provided" );
  return;
}

int posizione2 = Integer.parseInt(data.getString(Intent.EXTRA_TEXT));

The getIntent() method will give you the intent with which the calling activity had been started. No the intent holding the result of the called activity.

If you are getting the No text provided message, that means you have not properly returned the result in the activity that computes it.

Otros consejos

Try to use data.getStringExtra(Intent.EXTRA_TEXT)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top