0

I'm making an Android quiz and one activity stores every question. Now the score displays on that activity and I would like it if it that score goes to a new activity.. and displays..

code in the quiz:

Intent theIntent = new Intent(this, Score.class);
            theIntent.putExtra("somename", score);  
            startActivity(theIntent);

code in the score:

int i = getIntent().getIntExtra("somename");

I get an error on this word.. "getIntExtra" above in the score activity

4

2 回答 2

4

getIntExtra() takes a second parameter (default value in case the extra can't be found):

http://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String, int)

int i = getIntent().getIntExtra("somename", 0);

would do the job

于 2013-07-31T19:01:52.320 回答
1

You must provide a default value in the case that the integer doesn't exist in the intent extras.

Javadoc:

public int getIntExtra (String name, int defaultValue) Retrieve extended data from the intent.

Parameters:

name: The name of the desired item.

defaultValue: the value to be returned if no value of the desired type is stored with the given name.

Returns the value of an item that previously added with putExtra() or the default value if none was found.

于 2013-07-31T19:03:28.117 回答