Lorenzo
Lorenzo

Reputation: 117

Can't pass a var to an other activity

I have a problem to pass a var to an other activity: This is where I take the variable:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scelta);               
    // l'intent di questa activity
    Intent intent=getIntent();
    String pkg = getPackageName();
    //prendiamo i dati         
    nome=intent.getStringExtra(pkg+".myNome");

This is where I change activity:

public void checkout (View v){
    // l'intent di questa activity
    Intent intent;
    String pkg=getPackageName();
    intent=new Intent(getApplicationContext(), checkout.class);
    //li reinseriamo nell'intent
    intent.putExtra(pkg+".myNome", nome);
    intent.putExtra(pkg+".myId", id);

    Log.d(TAG,"questo è il nome che va ad instargh:"+nome);
    startActivity(intent);
}

thanks to log I am certain that "nome" have the value that i want. This is where i take "myNome"

public class checkout<targhe> extends ListActivity
{String sId=null;
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.checkout); 
    Intent intent;
    String pkg=getPackageName();                    
    intent=new Intent(getApplicationContext(), scelta.class);
    nome=intent.getStringExtra(pkg+".myNome");
    Log.D(TAG,"name"+nome);}

with this Log "nome" is always null. I don't understand why I can't take myNome from the intent :/

this is not all the code,but the parts the is important for my problem.

Some one can help me?

Upvotes: 1

Views: 94

Answers (3)

Apurva Pagare
Apurva Pagare

Reputation: 26

You should use getIntent().getStringExtra(pkg+".myNome");

Upvotes: 0

jmhend
jmhend

Reputation: 537

In checkout, try changing

Intent intent;                  
intent=new Intent(getApplicationContext(), scelta.class);

to

Intent intent = getIntent();

And see if that works.

Also, are both of your Activitys in the same package? If not, getPackageName() will be different in both.

Upvotes: 1

Vipul
Vipul

Reputation: 28093

Carefully see your code

in checkout Activity you have written

nome=intent.getStringExtra(pkg+".myNome");

Replace above line of code with following

nome=getIntent().getStringExtra(pkg+".myNome");

Upvotes: 4

Related Questions