Mario
Mario

Reputation: 14780

getIntExtra returns only the default value and getStringExtra works

I have this code which calls the activity

            Intent intent;
            intent=new Intent(TopicsActivity.this, DisplayTopicActivity.class);

            long nID=5;
            intent.putExtra("TOPIC_ID", nID);

            String strTopicName = ((TextView) viewClicked).getText().toString();
            intent.putExtra("TOPIC_NAME", strTopicName);

            startActivity(intent);

and this code in the activity

            private void handleIntent(Intent intent) {
            long nID = intent.getIntExtra("TOPIC_ID", 0);
            String strTopicName = intent.getStringExtra("TOPIC_NAME");

strTopicName is returned correctly by getStringExtra, but getIntExtra is returning 0 all the time

Upvotes: 2

Views: 3678

Answers (1)

David Wasser
David Wasser

Reputation: 95578

There is no Integer extra. That's why you are always getting zero. Look at the code that you use to put the extra there:

long nID=id;
intent.putExtra("TOPIC_ID", nID);

The extra with the key "TOPIC_ID" is a Long, not an Integer.

Either put it in as an Integer, or get it out as a Long.

Upvotes: 4

Related Questions